Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into 1.11.X
Browse files Browse the repository at this point in the history
  • Loading branch information
boq committed Dec 28, 2017
2 parents f71ba56 + c9117e5 commit 747f7a7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod_version=0.11.3
mod_version=0.11.4
next_mod_version=0.12
mc_ver=1.11.2
forge_ver=13.20.1.2386
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/openmods/LibConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class LibConfig {
@ConfigProperty(category = "debug", name = "fakePlayerCountThreshold", comment = "Maximum fake player pool that doesn't produce warning")
public static int fakePlayerThreshold = 10;

@OnLineModifiable
@ConfigProperty(category = "feature", name = "fakePlayerBlockBreakTools", comment = "List of tools used for checking effective during block breaking")
public static String[] toolProbes = new String[] { "minecraft:diamond_pickaxe", "minecraft:diamond_shovel", "minecraft:diamond_axe", "minecraft:diamond_sword", "minecraft:shears" };

@OnLineModifiable
@ConfigProperty(category = "debug", name = "dropsDebug", comment = "Control printing of stacktraces in case of unharvested drops")
public static boolean dropsDebug;
Expand Down
94 changes: 90 additions & 4 deletions src/main/java/openmods/fakeplayer/BreakBlockAction.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package openmods.fakeplayer;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import openmods.LibConfig;
import openmods.Log;
import openmods.config.properties.ConfigurationChange;
import openmods.fakeplayer.FakePlayerPool.PlayerUserReturning;
import openmods.world.DropCapture;
import openmods.world.DropCapture.CaptureContext;
Expand All @@ -25,17 +36,92 @@ public class BreakBlockAction implements PlayerUserReturning<List<EntityItem>> {
@Nonnull
private ItemStack stackToUse;

private boolean findEffectiveTool;

@EventBusSubscriber
private static class ConfigAccess {
private static ItemStack[] probeTools;

@SubscribeEvent
public static void onConfigUpdate(ConfigurationChange evt) {
if (evt.check("feature", "fakePlayerBlockBreakTools")) {
probeTools = null;
effectiveToolCache.invalidateAll();
}
}

public static ItemStack[] probeTools() {
if (probeTools == null) {
final List<ItemStack> items = Lists.newArrayList();
for (String itemId : LibConfig.toolProbes) {
final Item item = Item.REGISTRY.getObject(new ResourceLocation(itemId));
if (item != null) {
items.add(createToolStack(item));
} else {
Log.warn("Failed to find item: %s", itemId);
}
}
probeTools = items.toArray(new ItemStack[items.size()]);
}

return probeTools;
}
}

private static Cache<IBlockState, ItemStack> effectiveToolCache = CacheBuilder.newBuilder()
.expireAfterAccess(1, TimeUnit.HOURS)
.build();

public BreakBlockAction(World worldObj, BlockPos blockPos) {
this.worldObj = worldObj;
this.blockPos = blockPos;
this.stackToUse = new ItemStack(Items.DIAMOND_PICKAXE, 1, 0);
this.stackToUse = createToolStack(Items.DIAMOND_PICKAXE);
}

private static ItemStack createToolStack(Item tool) {
return new ItemStack(tool, 1, 0);
}

public BreakBlockAction setStackToUse(@Nonnull ItemStack stack) {
this.stackToUse = stack;
return this;
}

public BreakBlockAction findEffectiveTool() {
this.findEffectiveTool = true;
return this;
}

private void selectTool(IBlockState state, OpenModsFakePlayer fakePlayer) {
if (findEffectiveTool) {
final ItemStack optimalTool = effectiveToolCache.getIfPresent(state);

if (optimalTool != null) {
setPlayerTool(fakePlayer, optimalTool);
} else {
for (ItemStack tool : ConfigAccess.probeTools()) {
setPlayerTool(fakePlayer, tool);

if (ForgeHooks.canHarvestBlock(state.getBlock(), fakePlayer, worldObj, blockPos)) {
effectiveToolCache.put(state, tool);
return;
}
}

// default clause - use most universal one
final ItemStack fallbackTool = createToolStack(Items.DIAMOND_PICKAXE);
effectiveToolCache.put(state, fallbackTool);
setPlayerTool(fakePlayer, fallbackTool);
}
} else {
setPlayerTool(fakePlayer, stackToUse);
}
}

private static void setPlayerTool(OpenModsFakePlayer fakePlayer, final ItemStack tool) {
fakePlayer.inventory.setInventorySlotContents(0, tool.copy());
}

private boolean removeBlock(EntityPlayer player, BlockPos pos, IBlockState state, boolean canHarvest) {
final Block block = state.getBlock();
block.onBlockHarvested(worldObj, pos, state, player);
Expand All @@ -46,14 +132,14 @@ private boolean removeBlock(EntityPlayer player, BlockPos pos, IBlockState state

@Override
public List<EntityItem> usePlayer(OpenModsFakePlayer fakePlayer) {
fakePlayer.inventory.currentItem = 0;
fakePlayer.inventory.setInventorySlotContents(0, stackToUse);

if (!worldObj.isBlockModifiable(fakePlayer, blockPos)) return Lists.newArrayList();

// this mirrors ItemInWorldManager.tryHarvestBlock
final IBlockState state = worldObj.getBlockState(blockPos);

fakePlayer.inventory.currentItem = 0;
selectTool(state, fakePlayer);

final CaptureContext dropsCapturer = DropCapture.instance.start(blockPos);

final List<EntityItem> drops;
Expand Down

0 comments on commit 747f7a7

Please sign in to comment.