Skip to content

Commit

Permalink
+ Some tooltip stuff (more specific config)
Browse files Browse the repository at this point in the history
+ Change how tree harvesting works (more optimised)
+ Optimised some OreDictionaryHelper stuff

Signed-off-by: Dries007 <admin@dries007.net>
  • Loading branch information
dries007 committed Sep 29, 2018
1 parent 23aa11b commit ebf19fa
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 123 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/dries007/tfc/CommonEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.dries007.tfc.util.Helpers;
import net.dries007.tfc.util.IFireable;
import net.dries007.tfc.util.IPlacableItem;
import net.dries007.tfc.util.OreDictionaryHelper;

import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;
import static net.dries007.tfc.objects.blocks.BlockCharcoalPile.LAYERS;
Expand Down Expand Up @@ -88,7 +89,7 @@ If nothing happens (as per vanilla behavior, even if this event causes something
{
ItemStack mainStack = player.getHeldItem(EnumHand.MAIN_HAND);
if ((mainStack.getItem() == Items.COAL && mainStack.getMetadata() == 1) ||
(Helpers.doesStackMatchOre(mainStack, "logWood") && player.isSneaking()) ||
(OreDictionaryHelper.doesStackMatchOre(mainStack, "logWood") && player.isSneaking()) ||
mainStack.getItem() instanceof IPlacableItem)
{
event.setCanceled(true);
Expand Down Expand Up @@ -136,7 +137,7 @@ If nothing happens (as per vanilla behavior, even if this event causes something
}
}
}
if (Helpers.doesStackMatchOre(stack, "logWood") && player.isSneaking())
if (OreDictionaryHelper.doesStackMatchOre(stack, "logWood") && player.isSneaking())
{
EnumFacing facing = event.getFace();
if (facing != null)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/dries007/tfc/ConfigTFC.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public static class ClientCFG
@Config.LangKey("config." + MOD_ID + ".client.makeWorldTypeClassicDefault")
@Config.RequiresMcRestart
public boolean makeWorldTypeClassicDefault = true;

@Config.Comment({"Show ItemStack tool classes when advanced tooltips are enabled. (F3+H)"})
@Config.LangKey("config." + MOD_ID + ".client.showToolClassTooltip")
public boolean showToolClassTooltip = true;

@Config.Comment({"Show ItemStack OreDictionary matches when advanced tooltips are enabled. (F3+H)"})
@Config.LangKey("config." + MOD_ID + ".client.showOreDictionaryTooltip")
public boolean showOreDictionaryTooltip = true;

@Config.Comment({"Show ItemStack NBT on the tooltip when advanced tooltips are enabled. (F3+H)"})
@Config.LangKey("config." + MOD_ID + ".client.showNBTTooltip")
public boolean showNBTTooltip = false;
}

public static class WorldCFG
Expand Down
62 changes: 42 additions & 20 deletions src/main/java/net/dries007/tfc/client/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package net.dries007.tfc.client;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

Expand All @@ -17,7 +17,6 @@
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.WorldType;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.client.event.GuiScreenEvent;
Expand Down Expand Up @@ -135,6 +134,11 @@ public static void onRenderGameOverlayText(RenderGameOverlayEvent.Text event)
public static void onItemTooltip(ItemTooltipEvent event)
{
ItemStack stack = event.getItemStack();
if (stack.isEmpty())
{
TerraFirmaCraft.getLog().warn("ItemTooltipEvent with empty stack??", new Exception());
return;
}
Item item = stack.getItem();
List<String> tt = event.getToolTip();

Expand All @@ -153,7 +157,9 @@ public static void onItemTooltip(ItemTooltipEvent event)
if (event.getFlags().isAdvanced()) // Only added with advanced tooltip mode
{
if (item instanceof IMetalObject)
{
((IMetalObject) item).addMetalInfo(stack, tt);
}
if (item instanceof ItemBlock)
{
Block block = ((ItemBlock) item).getBlock();
Expand All @@ -162,31 +168,47 @@ public static void onItemTooltip(ItemTooltipEvent event)
((IMetalObject) block).addMetalInfo(stack, tt);
}
}
}

if (ConfigTFC.GENERAL.debug) // Only added when debugging (data that the player should / does not need to see)
{
if (stack.hasTagCompound())
if (ConfigTFC.CLIENT.showToolClassTooltip)
{
tt.add("NBT: " + stack.getTagCompound().toString());
Set<String> toolClasses = item.getToolClasses(stack);
if (toolClasses.size() == 1)
{
tt.add(I18n.format("tfc.tooltip.toolclass", toolClasses.iterator().next()));
}
else if (toolClasses.size() > 1)
{
tt.add(I18n.format("tfc.tooltip.toolclasses"));
for (String toolClass : toolClasses)
{
tt.add("+ " + toolClass);
}
}
}

Set<String> toolClasses = item.getToolClasses(stack);
if (!toolClasses.isEmpty())
if (ConfigTFC.CLIENT.showOreDictionaryTooltip)
{
tt.add("");
for (String toolClass : toolClasses)
int[] ids = OreDictionary.getOreIDs(stack);
if (ids.length == 1)
{
tt.add(I18n.format("tfc.tooltip.oredictionaryentry", OreDictionary.getOreName(ids[0])));
}
else if (ids.length > 1)
{
tt.add(I18n.format("tfc.tooltip.toolclass", toolClass));
tt.add(I18n.format("tfc.tooltip.oredictionaryentries"));
ArrayList<String> names = new ArrayList<>(ids.length);
for (int id : ids)
{
names.add("+ " + OreDictionary.getOreName(id));
}
names.sort(null); // Natural order (String.compare)
tt.addAll(names);
}
}

int[] ids = OreDictionary.getOreIDs(stack);
if (ids != null && ids.length != 0)
if (ConfigTFC.CLIENT.showNBTTooltip)
{
tt.add("");
tt.add(TextFormatting.AQUA + "Ore Dictionary:");
Arrays.stream(ids).mapToObj(OreDictionary::getOreName).sorted().forEachOrdered(tt::add);
if (stack.hasTagCompound())
{
tt.add("NBT: " + stack.getTagCompound().toString());
}
}
}
}
Expand Down
86 changes: 36 additions & 50 deletions src/main/java/net/dries007/tfc/objects/blocks/wood/BlockLogTFC.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.minecraft.world.World;

import mcp.MethodsReturnNonnullByDefault;
import net.dries007.tfc.TerraFirmaCraft;
import net.dries007.tfc.api.types.Tree;
import net.dries007.tfc.util.Helpers;
import net.dries007.tfc.util.OreDictionaryHelper;
Expand Down Expand Up @@ -118,40 +117,29 @@ public void updateTick(World world, BlockPos pos, IBlockState state, Random rand
@Override
public void onExplosionDestroy(World worldIn, BlockPos pos, Explosion explosionIn)
{
// The itemstack is a cheeky hack
if (!worldIn.isRemote)
removeTree(worldIn, pos, null, ItemStack.EMPTY, 1);
if (worldIn.isRemote) return;
removeTree(worldIn, pos, null, ItemStack.EMPTY, false);
}

@Override
public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player, boolean willHarvest)
public void onBlockHarvested(World world, BlockPos pos, IBlockState state, EntityPlayer player)
{
if (state.getValue(PLACED) || world.isRemote)
return super.removedByPlayer(state, world, pos, player, willHarvest);

// Check if player has a valid tool
ItemStack stack = player.getHeldItemMainhand();
int flags = 0;
if (Helpers.doesStackMatchOrePrefix(stack, "axe"))
flags += 1; // axe
if (Helpers.doesStackMatchOrePrefix(stack, "hammer"))
flags += 2; // hammer
if (Helpers.doesStackMatchOre(stack, "axeStone") || Helpers.doesStackMatchOre(stack, "hammerStone"))
flags += 4; // stone

if ((flags & 1) != 0) // bit 1 = is axe, bit 2 = is hammer, bit 3 is stone tool
if (world.isRemote || state.getValue(PLACED)) return;
final ItemStack stack = player.getHeldItemMainhand();
final Set<String> toolClasses = stack.getItem().getToolClasses(stack);
if (toolClasses.contains("axe"))
{
// cut down the tree
return removeTree(world, pos, player, stack, flags);
removeTree(world, pos, player, stack,
OreDictionaryHelper.doesStackMatchOre(stack, "axeStone") ||
OreDictionaryHelper.doesStackMatchOre(stack, "hammerStone")
);
}
else if ((flags & 2) != 0)
else if (toolClasses.contains("hammer")) //
{
// Break log and spawn some sticks
world.setBlockToAir(pos);
Helpers.spawnItemStack(world, pos.add(0.5D, 0.5D, 0.5D), new ItemStack(Items.STICK, 1 + (int) (Math.random() * 3)));
return true;
}
return super.removedByPlayer(state, world, pos, player, willHarvest);
}

@Override
Expand Down Expand Up @@ -184,58 +172,56 @@ public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing
return super.getStateForPlacement(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer).withProperty(PLACED, true).withProperty(SMALL, placer.isSneaking());
}

private boolean removeTree(World world, BlockPos pos, @Nullable EntityPlayer player, ItemStack stack, int flags)
private boolean removeTree(World world, BlockPos pos, @Nullable EntityPlayer player, ItemStack stack, boolean stoneTool)
{
TerraFirmaCraft.getLog().debug("Natural log harvesting");
int maxLogs = Integer.MAX_VALUE;
if (!stack.isEmpty())
maxLogs = 1 + stack.getMaxDamage() - stack.getItemDamage();

// find all logs and add them to a list
List<BlockPos> logs = new ArrayList<>();
BlockPos pos1, pos2;
final boolean explosion = stack.isEmpty() || player == null;
final int maxLogs = explosion ? Integer.MAX_VALUE : 1 + stack.getMaxDamage() - stack.getItemDamage();

// Find all logs and add them to a list
List<BlockPos> logs = new ArrayList<>(50);
List<BlockPos> checked = new ArrayList<>(50 * 3 * 3);
logs.add(pos);
for (int i = 0; i < logs.size(); i++)
{
pos1 = logs.get(i);
final BlockPos pos1 = logs.get(i);
// check for nearby logs
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
for (int z = -1; z <= 1; z++)
{
pos2 = pos1.add(x, y, z);
final BlockPos pos2 = pos1.add(x, y, z);
if (checked.contains(pos2)) continue;
checked.add(pos2);
IBlockState state = world.getBlockState(pos2);
if (state.getBlock() == this && !state.getValue(PLACED) && !logs.contains(pos2))
if (state.getBlock() == this && !state.getValue(PLACED))
logs.add(pos2);
}
}
}
}
TerraFirmaCraft.getLog().debug("Logs to break + {} in: {}", maxLogs, logs);
// sort the list in terms of max distance to the original tree
// Sort the list in terms of max distance to the original tree
logs.sort(Comparator.comparing(x -> x.distanceSq(pos)));
Collections.reverse(logs);
// start removing logs*/
for (int i = 0; i < Math.min(logs.size(), maxLogs); i++)

// Start removing logs*/
for (final BlockPos pos1 : logs.subList(0, Math.min(logs.size(), maxLogs)))
{
// Remove the top log of the list
pos1 = logs.get(i);
if (!stack.isEmpty() && player != null)
if (explosion)
{
if ((flags & 4) == 0 || Math.random() < 0.6) // Stone tools are 60% efficient
harvestBlock(world, player, pos1, world.getBlockState(pos1), null, stack);
stack.damageItem(1, player);
// Explosions are 30% Efficient: no TNT powered tree farms.
if (Math.random() < 0.3)
Helpers.spawnItemStack(world, pos.add(0.5d, 0.5d, 0.5d), new ItemStack(Item.getItemFromBlock(this)));
}
else
{
if (Math.random() < 0.3) // Explosions are 30% Efficient: no TNT powered tree farms.
Helpers.spawnItemStack(world, pos.add(0.5d, 0.5d, 0.5d), new ItemStack(Item.getItemFromBlock(this)));
// Stone tools are 60% efficient
if (!stoneTool || Math.random() < 0.6)
harvestBlock(world, player, pos1, world.getBlockState(pos1), null, stack);
stack.damageItem(1, player);
}
world.setBlockToAir(pos1);
}
return maxLogs >= logs.size();
}

}
3 changes: 2 additions & 1 deletion src/main/java/net/dries007/tfc/objects/te/TELogPile.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.dries007.tfc.objects.blocks.BlocksTFC;
import net.dries007.tfc.objects.blocks.wood.BlockLogPile;
import net.dries007.tfc.util.Helpers;
import net.dries007.tfc.util.OreDictionaryHelper;

import static net.dries007.tfc.api.util.TFCConstants.MOD_ID;
import static net.dries007.tfc.objects.blocks.BlockCharcoalPile.LAYERS;
Expand All @@ -38,7 +39,7 @@ public class TELogPile extends TESidedInventory implements ITickable

public static boolean isStackValid(ItemStack stack)
{
return (stack.isEmpty() || Helpers.doesStackMatchOre(stack, "logWood"));
return (stack.isEmpty() || OreDictionaryHelper.doesStackMatchOre(stack, "logWood"));
}

private final int maxBurnTicks = 8000; // 8 In-game Hours
Expand Down
50 changes: 3 additions & 47 deletions src/main/java/net/dries007/tfc/util/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.registries.IForgeRegistryEntry;

import net.dries007.tfc.api.types.Rock;
Expand Down Expand Up @@ -123,51 +121,9 @@ public static ItemStack consumeItem(ItemStack stack, EntityPlayer player, int am
return player.isCreative() ? stack : consumeItem(stack, amount);
}

// Checks if an itemstack has the ore name 'name'
public static boolean doesStackMatchOre(@Nonnull ItemStack stack, String name)
{
if (stack.isEmpty()) return false;
int[] ids = OreDictionary.getOreIDs(stack);
for (int id : ids)
{
String oreName = OreDictionary.getOreName(id);
if (name.equals(oreName))
{
return true;
}
}
return false;
}

// Checks is an ItemStack has ore names, which have a certain prefix
// used to search for all 'ingots' / all 'plates' etc.
public static boolean doesStackMatchOrePrefix(@Nonnull ItemStack stack, String prefix)
{
if (stack.isEmpty()) return false;
int[] ids = OreDictionary.getOreIDs(stack);
for (int id : ids)
{
String oreName = OreDictionary.getOreName(id);
if (oreName.length() >= prefix.length())
{
if (oreName.substring(0, prefix.length()).equals(prefix))
{
return true;
}
}
}
return false;
}

// This both checks if an ore dictionary entry exists, and it it has at least one itemstack
public static boolean doesOreHaveStack(String ore)
{
if (!OreDictionary.doesOreNameExist(ore)) return false;
NonNullList<ItemStack> stacks = OreDictionary.getOres(ore);
return !stacks.isEmpty();
}

// Simple method to spawn items in the world at a precise location, rather than using InventoryHelper
/**
* Simple method to spawn items in the world at a precise location, rather than using InventoryHelper
*/
public static void spawnItemStack(World world, BlockPos pos, ItemStack stack)
{
if (stack.isEmpty())
Expand Down
Loading

0 comments on commit ebf19fa

Please sign in to comment.