diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/ClientRegistryHandler.java b/src/main/java/de/ellpeck/actuallyadditions/common/ClientRegistryHandler.java index faf1c5b0e..2f2540f4b 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/ClientRegistryHandler.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/ClientRegistryHandler.java @@ -6,7 +6,7 @@ import de.ellpeck.actuallyadditions.common.blocks.render.IHasModel; import de.ellpeck.actuallyadditions.common.fluids.InitFluids; import de.ellpeck.actuallyadditions.common.util.FluidStateMapper; import net.minecraft.block.Block; -import net.minecraft.client.renderer.block.model.ModelBakery; +import net.minecraft.client.renderer.model.ModelBakery; import net.minecraft.client.renderer.model.ModelResourceLocation; import net.minecraft.fluid.Fluid; import net.minecraft.item.Item; @@ -15,8 +15,7 @@ import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.event.ModelBakeEvent; import net.minecraftforge.client.event.ModelRegistryEvent; import net.minecraftforge.client.model.ModelLoader; -import net.minecraftforge.fluids.Fluid; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/ItemDrill.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/ItemDrill.java index 5d8b66f55..6c924e1c7 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/ItemDrill.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/ItemDrill.java @@ -1,16 +1,10 @@ package de.ellpeck.actuallyadditions.common.items; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - import com.google.common.collect.Multimap; - import de.ellpeck.actuallyadditions.common.ActuallyAdditions; import de.ellpeck.actuallyadditions.common.blocks.metalists.TheColoredLampColors; import de.ellpeck.actuallyadditions.common.config.values.ConfigStringListValues; import de.ellpeck.actuallyadditions.common.inventory.ContainerDrill; -import de.ellpeck.actuallyadditions.common.inventory.GuiHandler; import de.ellpeck.actuallyadditions.common.items.base.ItemEnergy; import de.ellpeck.actuallyadditions.common.tile.TileEntityInventoryBase; import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA; @@ -18,47 +12,38 @@ import de.ellpeck.actuallyadditions.common.util.ItemUtil; import de.ellpeck.actuallyadditions.common.util.StackUtil; import de.ellpeck.actuallyadditions.common.util.WorldUtil; import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; import net.minecraft.block.material.Material; -import net.minecraft.block.state.IBlockState; -import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.entity.EntityLivingBase; +import net.minecraft.enchantment.Enchantments; +import net.minecraft.entity.LivingEntity; import net.minecraft.entity.SharedMonsterAttributes; import net.minecraft.entity.ai.attributes.AttributeModifier; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.InventoryPlayer; -import net.minecraft.init.Blocks; -import net.minecraft.init.Enchantments; -import net.minecraft.inventory.EntityEquipmentSlot; -import net.minecraft.item.EnumRarity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.inventory.EquipmentSlotType; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.ActionResult; -import net.minecraft.util.EnumActionResult; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumFacing.Axis; -import net.minecraft.util.EnumHand; -import net.minecraft.util.NonNullList; -import net.minecraft.util.ResourceLocation; +import net.minecraft.item.ItemUseContext; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.*; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.BlockRayTraceResult; import net.minecraft.util.math.RayTraceResult; import net.minecraft.world.World; import net.minecraftforge.common.ForgeHooks; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; +import net.minecraftforge.common.ToolType; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandlerModifiable; +import java.util.List; + public class ItemDrill extends ItemEnergy { public static final int HARVEST_LEVEL = 4; private static final int ENERGY_USE = 100; - public ItemDrill(String name) { - super(250000, 1000, name); - this.setMaxDamage(0); - this.setHasSubtypes(true); - this.setHarvestLevel("shovel", HARVEST_LEVEL); - this.setHarvestLevel("pickaxe", HARVEST_LEVEL); + public ItemDrill(Properties properties, String name) { + super(properties.maxDamage(0).addToolType(ToolType.SHOVEL, HARVEST_LEVEL).addToolType(ToolType.PICKAXE, HARVEST_LEVEL), 250000, 1000, name); } /** @@ -67,10 +52,8 @@ public class ItemDrill extends ItemEnergy { * @param stack The Drill */ public static void loadSlotsFromNBT(IItemHandlerModifiable slots, ItemStack stack) { - NBTTagCompound compound = stack.getTagCompound(); - if (compound != null) { - TileEntityInventoryBase.loadSlots(slots, compound); - } + CompoundNBT compound = stack.getOrCreateTag(); + TileEntityInventoryBase.loadSlots(slots, compound); } /** @@ -80,52 +63,49 @@ public class ItemDrill extends ItemEnergy { * @param stack The Drill */ public static void writeSlotsToNBT(IItemHandler slots, ItemStack stack) { - NBTTagCompound compound = stack.getTagCompound(); - if (compound == null) { - compound = new NBTTagCompound(); - } + CompoundNBT compound = stack.getOrCreateTag(); TileEntityInventoryBase.saveSlots(slots, compound); - stack.setTagCompound(compound); + stack.setTag(compound); } @Override - //Places Blocks if the Placing Upgrade is installed - public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { - ItemStack stack = player.getHeldItem(hand); + public ActionResultType onItemUse(ItemUseContext context) { + ItemStack stack = context.getPlayer().getHeldItem(context.getHand()); ItemStack upgrade = this.getHasUpgradeAsStack(stack, ItemDrillUpgrade.UpgradeType.PLACER); if (StackUtil.isValid(upgrade)) { int slot = ItemDrillUpgrade.getSlotToPlaceFrom(upgrade); - if (slot >= 0 && slot < InventoryPlayer.getHotbarSize()) { - ItemStack equip = player.inventory.getStackInSlot(slot); + if (slot >= 0 && slot < PlayerInventory.getHotbarSize()) { + ItemStack equip = context.getPlayer().inventory.getStackInSlot(slot); if (StackUtil.isValid(equip) && equip != stack) { ItemStack toPlaceStack = equip.copy(); - WorldUtil.setHandItemWithoutAnnoyingSound(player, hand, toPlaceStack); + WorldUtil.setHandItemWithoutAnnoyingSound(context.getPlayer(), context.getHand(), toPlaceStack); //tryPlaceItemIntoWorld could throw an Exception try { //Places the Block into the World - if (toPlaceStack.onItemUse(player, world, pos, hand, side, hitX, hitY, hitZ) != EnumActionResult.FAIL) { - if (!player.capabilities.isCreativeMode) { - WorldUtil.setHandItemWithoutAnnoyingSound(player, hand, toPlaceStack.copy()); + if (toPlaceStack.onItemUse(context) != ActionResultType.FAIL) { + if (!context.getPlayer().isCreative()) { + WorldUtil.setHandItemWithoutAnnoyingSound(context.getPlayer(), context.getHand(), toPlaceStack.copy()); } } } //Notify the Player and log the Exception catch (Exception e) { - ActuallyAdditions.LOGGER.error("Player " + player.getName() + " who should place a Block using a Drill at " + player.posX + ", " + player.posY + ", " + player.posZ + " in World " + world.provider.getDimension() + " threw an Exception! Don't let that happen again!"); + ActuallyAdditions.LOGGER.error("Player " + context.getPlayer().getName() + " who should place a Block using a Drill at " + context.getPlayer().getPosX() + ", " + context.getPlayer().getPosY() + ", " + context.getPlayer().getPosY() + " in World " + context.getWorld().getDimension() + " threw an Exception! Don't let that happen again!"); } - player.inventory.setInventorySlotContents(slot, player.getHeldItem(hand)); - WorldUtil.setHandItemWithoutAnnoyingSound(player, hand, stack); + context.getPlayer().inventory.setInventorySlotContents(slot, context.getPlayer().getHeldItem(context.getHand())); + WorldUtil.setHandItemWithoutAnnoyingSound(context.getPlayer(), context.getHand(), stack); - return EnumActionResult.SUCCESS; + return ActionResultType.SUCCESS; } } } - return EnumActionResult.FAIL; + return ActionResultType.FAIL; } + /** * Checks if a certain Upgrade is installed and returns it as an ItemStack * @@ -134,7 +114,7 @@ public class ItemDrill extends ItemEnergy { * @return The Upgrade, if it's installed */ public ItemStack getHasUpgradeAsStack(ItemStack stack, ItemDrillUpgrade.UpgradeType upgrade) { - NBTTagCompound compound = stack.getTagCompound(); + CompoundNBT compound = stack.getOrCreateTag(); if (compound == null) { return StackUtil.getEmpty(); } ItemStackHandlerAA inv = new ItemStackHandlerAA(ContainerDrill.SLOT_AMOUNT); @@ -149,22 +129,18 @@ public class ItemDrill extends ItemEnergy { } @Override - public ActionResult onItemRightClick(World world, EntityPlayer player, EnumHand hand) { - if (!world.isRemote && player.isSneaking() && hand == EnumHand.MAIN_HAND) { - player.openGui(ActuallyAdditions.INSTANCE, GuiHandler.GuiTypes.DRILL.ordinal(), world, (int) player.posX, (int) player.posY, (int) player.posZ); + public ActionResult onItemRightClick(World world, PlayerEntity player, Hand hand) { + // todo: reimplement + if (!world.isRemote && player.isSneaking() && hand == Hand.MAIN_HAND) { +// player.openGui(ActuallyAdditions.INSTANCE, GuiHandler.GuiTypes.DRILL.ordinal(), world, (int) player.posX, (int) player.posY, (int) player.posZ); } - return new ActionResult<>(EnumActionResult.PASS, player.getHeldItem(hand)); + return new ActionResult<>(ActionResultType.PASS, player.getHeldItem(hand)); } @Override - public int getMetadata(int damage) { - return damage; - } - - @Override - public boolean hitEntity(ItemStack stack, EntityLivingBase entity1, EntityLivingBase entity2) { + public boolean hitEntity(ItemStack stack, LivingEntity target, LivingEntity attacker) { int use = this.getEnergyUsePerBlock(stack); - if (!(entity2 instanceof EntityPlayer) || !((EntityPlayer) entity2).capabilities.isCreativeMode) { + if (!(attacker instanceof PlayerEntity) || !((PlayerEntity) attacker).isCreative()) { if (this.getEnergyStored(stack) >= use) { this.extractEnergyInternal(stack, use, false); } @@ -173,29 +149,31 @@ public class ItemDrill extends ItemEnergy { } @Override - public EnumRarity getRarity(ItemStack stack) { - return EnumRarity.EPIC; - } - - @Override - public Multimap getAttributeModifiers(EntityEquipmentSlot slot, ItemStack stack) { + public Multimap getAttributeModifiers(EquipmentSlotType slot, ItemStack stack) { Multimap map = super.getAttributeModifiers(slot, stack); - if (slot == EntityEquipmentSlot.MAINHAND) { - map.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Drill Modifier", this.getEnergyStored(stack) >= ENERGY_USE ? 8.0F : 0.1F, 0)); - map.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(ATTACK_SPEED_MODIFIER, "Tool Modifier", -2.5F, 0)); + // todo: operation might be wrong here + if (slot == EquipmentSlotType.MAINHAND) { + map.put(SharedMonsterAttributes.ATTACK_DAMAGE.getName(), new AttributeModifier(ATTACK_DAMAGE_MODIFIER, "Drill Modifier", this.getEnergyStored(stack) >= ENERGY_USE ? 8.0F : 0.1F, AttributeModifier.Operation.ADDITION)); + map.put(SharedMonsterAttributes.ATTACK_SPEED.getName(), new AttributeModifier(ATTACK_SPEED_MODIFIER, "Tool Modifier", -2.5F, AttributeModifier.Operation.ADDITION)); } return map; } @Override - public float getDestroySpeed(ItemStack stack, IBlockState state) { - return this.getEnergyStored(stack) >= this.getEnergyUsePerBlock(stack) ? this.hasExtraWhitelist(state.getBlock()) || state.getBlock().getHarvestTool(state) == null || state.getBlock().getHarvestTool(state).isEmpty() || this.getToolClasses(stack).contains(state.getBlock().getHarvestTool(state)) ? this.getEfficiencyFromUpgrade(stack) : 1.0F : 0.1F; + public float getDestroySpeed(ItemStack stack, BlockState state) { + return this.getEnergyStored(stack) >= this.getEnergyUsePerBlock(stack) + ? (this.hasExtraWhitelist(state.getBlock()) + || state.getBlock().getHarvestTool(state) == null + || this.getToolTypes(stack).contains(state.getBlock().getHarvestTool(state))) + ? this.getEfficiencyFromUpgrade(stack) + : 1.0F + : 0.1F; } @Override - public boolean onBlockStartBreak(ItemStack stack, BlockPos pos, EntityPlayer player) { + public boolean onBlockStartBreak(ItemStack stack, BlockPos pos, PlayerEntity player) { boolean toReturn = false; int use = this.getEnergyUsePerBlock(stack); if (this.getEnergyStored(stack) >= use) { @@ -215,12 +193,12 @@ public class ItemDrill extends ItemEnergy { //Breaks the Blocks if (!player.isSneaking() && this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.THREE_BY_THREE)) { if (this.getHasUpgrade(stack, ItemDrillUpgrade.UpgradeType.FIVE_BY_FIVE)) { - toReturn = this.breakBlocks(stack, 2, player.world, pos, ray.sideHit, player); + toReturn = this.breakBlocks(stack, 2, player.world, pos, ((BlockRayTraceResult)ray).getFace(), player); } else { - toReturn = this.breakBlocks(stack, 1, player.world, pos, ray.sideHit, player); + toReturn = this.breakBlocks(stack, 1, player.world, pos, ((BlockRayTraceResult)ray).getFace(), player); } } else { - toReturn = this.breakBlocks(stack, 0, player.world, pos, ray.sideHit, player); + toReturn = this.breakBlocks(stack, 0, player.world, pos, ((BlockRayTraceResult)ray).getFace(), player); } //Removes Enchantments added above @@ -232,22 +210,9 @@ public class ItemDrill extends ItemEnergy { } @Override - public boolean canHarvestBlock(IBlockState state, ItemStack stack) { + public boolean canHarvestBlock(ItemStack stack, BlockState state) { Block block = state.getBlock(); - return this.getEnergyStored(stack) >= this.getEnergyUsePerBlock(stack) && (this.hasExtraWhitelist(block) || state.getMaterial().isToolNotRequired() || block == Blocks.SNOW_LAYER || block == Blocks.SNOW || (block == Blocks.OBSIDIAN ? HARVEST_LEVEL >= 3 : block != Blocks.DIAMOND_BLOCK && block != Blocks.DIAMOND_ORE ? block != Blocks.EMERALD_ORE && block != Blocks.EMERALD_BLOCK ? block != Blocks.GOLD_BLOCK && block != Blocks.GOLD_ORE ? block != Blocks.IRON_BLOCK && block != Blocks.IRON_ORE ? block != Blocks.LAPIS_BLOCK && block != Blocks.LAPIS_ORE ? block != Blocks.REDSTONE_ORE && block != Blocks.LIT_REDSTONE_ORE ? state.getMaterial() == Material.ROCK || state.getMaterial() == Material.IRON || state.getMaterial() == Material.ANVIL : HARVEST_LEVEL >= 2 : HARVEST_LEVEL >= 1 : HARVEST_LEVEL >= 1 : HARVEST_LEVEL >= 2 : HARVEST_LEVEL >= 2 : HARVEST_LEVEL >= 2)); - } - - @Override - public Set getToolClasses(ItemStack stack) { - HashSet hashSet = new HashSet<>(); - hashSet.add("pickaxe"); - hashSet.add("shovel"); - return hashSet; - } - - @Override - public int getHarvestLevel(ItemStack stack, String toolClass, EntityPlayer player, IBlockState blockState) { - return HARVEST_LEVEL; + return this.getEnergyStored(stack) >= this.getEnergyUsePerBlock(stack) && (this.hasExtraWhitelist(block) || state.getMaterial().isToolNotRequired() || block == Blocks.SNOW_BLOCK || block == Blocks.SNOW || (block == Blocks.OBSIDIAN ? HARVEST_LEVEL >= 3 : block != Blocks.DIAMOND_BLOCK && block != Blocks.DIAMOND_ORE ? block != Blocks.EMERALD_ORE && block != Blocks.EMERALD_BLOCK ? block != Blocks.GOLD_BLOCK && block != Blocks.GOLD_ORE ? block != Blocks.IRON_BLOCK && block != Blocks.IRON_ORE ? block != Blocks.LAPIS_BLOCK && block != Blocks.LAPIS_ORE ? block != Blocks.REDSTONE_ORE ? state.getMaterial() == Material.ROCK || state.getMaterial() == Material.IRON || state.getMaterial() == Material.ANVIL : HARVEST_LEVEL >= 2 : HARVEST_LEVEL >= 1 : HARVEST_LEVEL >= 1 : HARVEST_LEVEL >= 2 : HARVEST_LEVEL >= 2 : HARVEST_LEVEL >= 2)); } /** @@ -309,26 +274,27 @@ public class ItemDrill extends ItemEnergy { protected void registerRendering() { for (int i = 0; i < 16; i++) { String name = this.getRegistryName() + "_" + TheColoredLampColors.values()[i].regName; - ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this, 1, i), new ResourceLocation(name), "inventory"); + ActuallyAdditions.PROXY.addRenderRegister(new ItemStack(this, 1), new ResourceLocation(name), "inventory"); } } - @Override - @SideOnly(Side.CLIENT) - public void getSubItems(CreativeTabs tabs, NonNullList list) { - if (this.isInCreativeTab(tabs)) { - for (int i = 0; i < 16; i++) { - this.addDrillStack(list, i); - } - } - } +// @Override +// @SideOnly(Side.CLIENT) +// public void getSubItems(CreativeTabs tabs, NonNullList list) { +// if (this.isInCreativeTab(tabs)) { +// for (int i = 0; i < 16; i++) { +// this.addDrillStack(list, i); +// } +// } +// } + // todo: fix this private void addDrillStack(List list, int meta) { - ItemStack stackFull = new ItemStack(this, 1, meta); + ItemStack stackFull = new ItemStack(this, 1); this.setEnergy(stackFull, this.getMaxEnergyStored(stackFull)); list.add(stackFull); - ItemStack stackEmpty = new ItemStack(this, 1, meta); + ItemStack stackEmpty = new ItemStack(this, 1); this.setEnergy(stackEmpty, 0); list.add(stackEmpty); } @@ -370,23 +336,23 @@ public class ItemDrill extends ItemEnergy { * @param world The World * @param player The Player who breaks the Blocks */ - public boolean breakBlocks(ItemStack stack, int radius, World world, BlockPos aPos, EnumFacing side, EntityPlayer player) { + public boolean breakBlocks(ItemStack stack, int radius, World world, BlockPos aPos, Direction side, PlayerEntity player) { int xRange = radius; int yRange = radius; int zRange = 0; //Corrects Blocks to hit depending on Side of original Block hit - if (side.getAxis() == Axis.Y) { + if (side.getAxis() == Direction.Axis.Y) { zRange = radius; yRange = 0; } - if (side.getAxis() == Axis.X) { + if (side.getAxis() == Direction.Axis.X) { xRange = 0; zRange = radius; } //Not defined later because main Block is getting broken below - IBlockState state = world.getBlockState(aPos); + BlockState state = world.getBlockState(aPos); float mainHardness = state.getBlockHardness(world, aPos); //Break Middle Block first @@ -397,9 +363,9 @@ public class ItemDrill extends ItemEnergy { return false; } - if (radius == 2 && side.getAxis() != Axis.Y) { + if (radius == 2 && side.getAxis() != Direction.Axis.Y) { aPos = aPos.up(); - IBlockState theState = world.getBlockState(aPos); + BlockState theState = world.getBlockState(aPos); if (theState.getBlockHardness(world, aPos) <= mainHardness + 5.0F) { this.tryHarvestBlock(world, aPos, true, stack, player, use); } @@ -414,7 +380,7 @@ public class ItemDrill extends ItemEnergy { if (this.getEnergyStored(stack) >= use) { //Only break Blocks around that are (about) as hard or softer BlockPos thePos = new BlockPos(xPos, yPos, zPos); - IBlockState theState = world.getBlockState(thePos); + BlockState theState = world.getBlockState(thePos); if (theState.getBlockHardness(world, thePos) <= mainHardness + 5.0F) { this.tryHarvestBlock(world, thePos, true, stack, player, use); } @@ -440,13 +406,13 @@ public class ItemDrill extends ItemEnergy { * @param player The Player breaking the Blocks * @param use The Energy that should be extracted per Block */ - private boolean tryHarvestBlock(World world, BlockPos pos, boolean isExtra, ItemStack stack, EntityPlayer player, int use) { - IBlockState state = world.getBlockState(pos); + private boolean tryHarvestBlock(World world, BlockPos pos, boolean isExtra, ItemStack stack, PlayerEntity player, int use) { + BlockState state = world.getBlockState(pos); Block block = state.getBlock(); float hardness = state.getBlockHardness(world, pos); - boolean canHarvest = (ForgeHooks.canHarvestBlock(block, player, world, pos) || this.canHarvestBlock(state, stack)) && (!isExtra || this.getDestroySpeed(stack, world.getBlockState(pos)) > 1.0F); + boolean canHarvest = (ForgeHooks.canHarvestBlock(state, player, world, pos) || this.canHarvestBlock(stack, state)) && (!isExtra || this.getDestroySpeed(stack, world.getBlockState(pos)) > 1.0F); if (hardness >= 0.0F && (!isExtra || canHarvest && !block.hasTileEntity(world.getBlockState(pos)))) { - if (!player.capabilities.isCreativeMode) { + if (!player.isCreative()) { this.extractEnergyInternal(stack, use, false); } //Break the Block diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/base/ItemBase.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/base/ItemBase.java index 789d016da..a202fa2fd 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/base/ItemBase.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/base/ItemBase.java @@ -9,7 +9,8 @@ public class ItemBase extends Item { private final String name; - public ItemBase(String name) { + public ItemBase(Properties properties, String name) { + super(properties); this.name = name; this.register(); diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/items/base/ItemEnergy.java b/src/main/java/de/ellpeck/actuallyadditions/common/items/base/ItemEnergy.java index d4fe0d897..7646c5b1b 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/items/base/ItemEnergy.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/items/base/ItemEnergy.java @@ -1,82 +1,77 @@ package de.ellpeck.actuallyadditions.common.items.base; -import java.text.NumberFormat; -import java.util.List; - -import javax.annotation.Nullable; - import de.ellpeck.actuallyadditions.common.ActuallyAdditions; import de.ellpeck.actuallyadditions.common.tile.CustomEnergyStorage; import de.ellpeck.actuallyadditions.common.util.AssetUtil; import net.minecraft.client.util.ITooltipFlag; -import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.NonNullList; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.Direction; import net.minecraft.util.math.MathHelper; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.StringTextComponent; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.energy.IEnergyStorage; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.text.NumberFormat; +import java.util.List; public abstract class ItemEnergy extends ItemBase { private final int maxPower; private final int transfer; - public ItemEnergy(int maxPower, int transfer, String name) { - super(name); + public ItemEnergy(Properties properties, int maxPower, int transfer, String name) { + super(properties.maxStackSize(1), name); this.maxPower = maxPower; this.transfer = transfer; + } - this.setHasSubtypes(true); - this.setMaxStackSize(1); +// @Override +// public boolean getShareTag() { +// return true; +// } + + + @Override + public void addInformation(ItemStack stack, @Nullable World worldIn, List tooltip, ITooltipFlag flagIn) { + stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(cap -> { + NumberFormat format = NumberFormat.getInstance(); + // todo: migrate to i18n + tooltip.add(new StringTextComponent(String.format("%s/%s Crystal Flux", format.format(cap.getEnergyStored()), format.format(cap.getMaxEnergyStored())))); + }); } @Override - public boolean getShareTag() { - return true; - } - - @Override - public void addInformation(ItemStack stack, World playerIn, List tooltip, ITooltipFlag advanced) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage != null) { - NumberFormat format = NumberFormat.getInstance(); - tooltip.add(String.format("%s/%s Crystal Flux", format.format(storage.getEnergyStored()), format.format(storage.getMaxEnergyStored()))); - } - } - } - - @Override - @SideOnly(Side.CLIENT) public boolean hasEffect(ItemStack stack) { return false; } - @Override - public void getSubItems(CreativeTabs tabs, NonNullList list) { - if (this.isInCreativeTab(tabs)) { - ItemStack stackFull = new ItemStack(this); - if (stackFull.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stackFull.getCapability(CapabilityEnergy.ENERGY, null); - if (storage != null) { - this.setEnergy(stackFull, storage.getMaxEnergyStored()); - list.add(stackFull); - } - } - - ItemStack stackEmpty = new ItemStack(this); - this.setEnergy(stackEmpty, 0); - list.add(stackEmpty); - } - } +// todo: check this out +// @Override +// public void getSubItems(CreativeTabs tabs, NonNullList list) { +// if (this.isInCreativeTab(tabs)) { +// ItemStack stackFull = new ItemStack(this); +// if (stackFull.hasCapability(CapabilityEnergy.ENERGY, null)) { +// IEnergyStorage storage = stackFull.getCapability(CapabilityEnergy.ENERGY, null); +// if (storage != null) { +// this.setEnergy(stackFull, storage.getMaxEnergyStored()); +// list.add(stackFull); +// } +// } +// +// ItemStack stackEmpty = new ItemStack(this); +// this.setEnergy(stackEmpty, 0); +// list.add(stackEmpty); +// } +// } @Override public boolean showDurabilityBar(ItemStack itemStack) { @@ -85,103 +80,71 @@ public abstract class ItemEnergy extends ItemBase { @Override public double getDurabilityForDisplay(ItemStack stack) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage != null) { - double maxAmount = storage.getMaxEnergyStored(); - double energyDif = maxAmount - storage.getEnergyStored(); - return energyDif / maxAmount; - } - } - return super.getDurabilityForDisplay(stack); + return stack.getCapability(CapabilityEnergy.ENERGY) + .map(cap -> { + double maxAmount = cap.getMaxEnergyStored(); + double energyDif = maxAmount - cap.getEnergyStored(); + return energyDif / maxAmount; + }) + .orElse(super.getDurabilityForDisplay(stack)); } @Override public int getRGBDurabilityForDisplay(ItemStack stack) { - EntityPlayer player = ActuallyAdditions.PROXY.getCurrentPlayer(); + PlayerEntity player = ActuallyAdditions.PROXY.getCurrentPlayer(); if (player != null && player.world != null) { - float[] color = AssetUtil.getWheelColor(player.world.getTotalWorldTime() % 256); + float[] color = AssetUtil.getWheelColor(player.world.getGameTime() % 256); // todo: validate that getGameTime is correct (check diff for old) return MathHelper.rgb(color[0] / 255F, color[1] / 255F, color[2] / 255F); } return super.getRGBDurabilityForDisplay(stack); } public void setEnergy(ItemStack stack, int energy) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage instanceof CustomEnergyStorage) { - ((CustomEnergyStorage) storage).setEnergyStored(energy); - } - } + stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(cap -> ((CustomEnergyStorage) cap).setEnergyStored(energy)); } public int receiveEnergyInternal(ItemStack stack, int maxReceive, boolean simulate) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage instanceof CustomEnergyStorage) { - ((CustomEnergyStorage) storage).receiveEnergyInternal(maxReceive, simulate); - } - } + stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(cap -> ((CustomEnergyStorage) cap).receiveEnergyInternal(maxReceive, simulate)); return 0; } public int extractEnergyInternal(ItemStack stack, int maxExtract, boolean simulate) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage instanceof CustomEnergyStorage) { - ((CustomEnergyStorage) storage).extractEnergyInternal(maxExtract, simulate); - } - } + stack.getCapability(CapabilityEnergy.ENERGY).ifPresent(cap -> cap.extractEnergy(maxExtract, simulate)); return 0; } public int receiveEnergy(ItemStack stack, int maxReceive, boolean simulate) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage != null) { return storage.receiveEnergy(maxReceive, simulate); } - } - return 0; + return stack.getCapability(CapabilityEnergy.ENERGY).map(cap -> cap.receiveEnergy(maxReceive, simulate)).orElse(0); } public int extractEnergy(ItemStack stack, int maxExtract, boolean simulate) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage != null) { return storage.extractEnergy(maxExtract, simulate); } - } - return 0; + return stack.getCapability(CapabilityEnergy.ENERGY).map(cap -> cap.extractEnergy(maxExtract, simulate)).orElse(0); } public int getEnergyStored(ItemStack stack) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage != null) { return storage.getEnergyStored(); } - } - return 0; + return stack.getCapability(CapabilityEnergy.ENERGY).map(IEnergyStorage::getEnergyStored).orElse(0); } public int getMaxEnergyStored(ItemStack stack) { - if (stack.hasCapability(CapabilityEnergy.ENERGY, null)) { - IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY, null); - if (storage != null) { return storage.getMaxEnergyStored(); } - } - return 0; + return stack.getCapability(CapabilityEnergy.ENERGY).map(IEnergyStorage::getMaxEnergyStored).orElse(0); } @Override - public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt) { + public ICapabilityProvider initCapabilities(ItemStack stack, CompoundNBT nbt) { return new EnergyCapabilityProvider(stack, this); } private static class EnergyCapabilityProvider implements ICapabilityProvider { public final CustomEnergyStorage storage; + private final LazyOptional lazyStorage; public EnergyCapabilityProvider(final ItemStack stack, ItemEnergy item) { this.storage = new CustomEnergyStorage(item.maxPower, item.transfer, item.transfer) { @Override public int getEnergyStored() { - if (stack.hasTagCompound()) { - return stack.getTagCompound().getInteger("Energy"); + if (stack.hasTag()) { + return stack.getOrCreateTag().getInt("Energy"); } else { return 0; } @@ -189,25 +152,21 @@ public abstract class ItemEnergy extends ItemBase { @Override public void setEnergyStored(int energy) { - if (!stack.hasTagCompound()) { - stack.setTagCompound(new NBTTagCompound()); - } - - stack.getTagCompound().setInteger("Energy", energy); + stack.getOrCreateTag().putInt("Energy", energy); } }; + + lazyStorage = LazyOptional.of(() -> this.storage); } + @Nonnull @Override - public boolean hasCapability(Capability capability, EnumFacing facing) { - return this.getCapability(capability, facing) != null; - } + public LazyOptional getCapability(@Nonnull Capability cap, @Nullable Direction side) { + if (cap == CapabilityEnergy.ENERGY) { + return this.lazyStorage.cast(); + } - @Nullable - @Override - public T getCapability(Capability capability, EnumFacing facing) { - if (capability == CapabilityEnergy.ENERGY) { return CapabilityEnergy.ENERGY.cast(this.storage); } - return null; + return LazyOptional.empty(); } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/proxy/IProxy.java b/src/main/java/de/ellpeck/actuallyadditions/common/proxy/IProxy.java index 25b38c2a7..5f92ae759 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/proxy/IProxy.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/proxy/IProxy.java @@ -2,6 +2,7 @@ package de.ellpeck.actuallyadditions.common.proxy; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.ResourceLocation; @@ -24,7 +25,7 @@ public interface IProxy { void addColoredBlock(Block block); - EntityPlayer getCurrentPlayer(); + PlayerEntity getCurrentPlayer(); default void sendBreakPacket(BlockPos pos) { }; diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/tile/ISharingEnergyProvider.java b/src/main/java/de/ellpeck/actuallyadditions/common/tile/ISharingEnergyProvider.java index 068f5cf24..b0c205ea2 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/tile/ISharingEnergyProvider.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/tile/ISharingEnergyProvider.java @@ -1,7 +1,7 @@ package de.ellpeck.actuallyadditions.common.tile; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.EnumFacing; +import net.minecraft.util.Direction; public interface ISharingEnergyProvider { @@ -9,7 +9,7 @@ public interface ISharingEnergyProvider { boolean doesShareEnergy(); - EnumFacing[] getEnergyShareSides(); + Direction[] getEnergyShareSides(); boolean canShareTo(TileEntity tile); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/tile/ISharingFluidHandler.java b/src/main/java/de/ellpeck/actuallyadditions/common/tile/ISharingFluidHandler.java index 6b1c415ec..faf6213b9 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/tile/ISharingFluidHandler.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/tile/ISharingFluidHandler.java @@ -1,6 +1,6 @@ package de.ellpeck.actuallyadditions.common.tile; -import net.minecraft.util.EnumFacing; +import net.minecraft.util.Direction; public interface ISharingFluidHandler { @@ -8,6 +8,6 @@ public interface ISharingFluidHandler { boolean doesShareFluid(); - EnumFacing[] getFluidShareSides(); + Direction[] getFluidShareSides(); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityBase.java b/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityBase.java index 9e3a225ea..7b643ff70 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityBase.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityBase.java @@ -4,30 +4,28 @@ import de.ellpeck.actuallyadditions.common.ActuallyAdditions; import de.ellpeck.actuallyadditions.common.config.values.ConfigIntValues; import de.ellpeck.actuallyadditions.common.util.VanillaPacketDispatcher; import de.ellpeck.actuallyadditions.common.util.WorldUtil; -import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.CompoundNBT; -import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.NetworkManager; -import net.minecraft.network.play.server.SPacketUpdateTileEntity; +import net.minecraft.network.play.server.SUpdateTileEntityPacket; +import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.ITickable; -import net.minecraft.util.ResourceLocation; +import net.minecraft.tileentity.TileEntityType; +import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.text.ITextComponent; -import net.minecraft.util.text.TextComponentTranslation; -import net.minecraft.world.World; import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.energy.CapabilityEnergy; import net.minecraftforge.energy.IEnergyStorage; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; -public abstract class TileEntityBase extends TileEntity implements ITickable { +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public abstract class TileEntityBase extends TileEntity implements ITickableTileEntity { public final String name; public boolean isRedstonePowered; @@ -37,125 +35,129 @@ public abstract class TileEntityBase extends TileEntity implements ITickable { protected TileEntity[] tilesAround = new TileEntity[6]; protected boolean hasSavedDataOnChangeOrWorldStart; - public TileEntityBase(String name) { + public TileEntityBase(TileEntityType type, String name) { + super(type); this.name = name; } - public static void init() { - ActuallyAdditions.LOGGER.info("Registering TileEntities..."); +// public static void init() { +// ActuallyAdditions.LOGGER.info("Registering TileEntities..."); +// +// register(TileEntityCompost.class); +// register(TileEntityFeeder.class); +// register(TileEntityGiantChest.class); +// register(TileEntityGiantChestMedium.class); +// register(TileEntityGiantChestLarge.class); +// register(TileEntityGrinder.class); +// register(TileEntityFurnaceDouble.class); +// register(TileEntityInputter.class); +// register(TileEntityFishingNet.class); +// register(TileEntityFurnaceSolar.class); +// register(TileEntityHeatCollector.class); +// register(TileEntityItemRepairer.class); +// register(TileEntityBreaker.class); +// register(TileEntityDropper.class); +// register(TileEntityInputterAdvanced.class); +// register(TileEntityPlacer.class); +// register(TileEntityGrinderDouble.class); +// register(TileEntityCanolaPress.class); +// register(TileEntityFermentingBarrel.class); +// register(TileEntityOilGenerator.class); +// register(TileEntityCoalGenerator.class); +// register(TileEntityPhantomItemface.class); +// register(TileEntityPhantomLiquiface.class); +// register(TileEntityPhantomEnergyface.class); +// register(TileEntityPlayerInterface.class); +// register(TileEntityPhantomPlacer.class); +// register(TileEntityPhantomBreaker.class); +// register(TileEntityFluidCollector.class); +// register(TileEntityFluidPlacer.class); +// register(TileEntityLavaFactoryController.class); +// register(TileEntityCoffeeMachine.class); +// register(TileEntityPhantomBooster.class); +// register(TileEntityEnergizer.class); +// register(TileEntityEnervator.class); +// register(TileEntityXPSolidifier.class); +// register(TileEntitySmileyCloud.class); +// register(TileEntityLeafGenerator.class); +// register(TileEntityDirectionalBreaker.class); +// register(TileEntityRangedCollector.class); +// register(TileEntityAtomicReconstructor.class); +// register(TileEntityMiner.class); +// register(TileEntityFireworkBox.class); +// register(TileEntityPhantomRedstoneface.class); +// register(TileEntityLaserRelayItem.class); +// register(TileEntityLaserRelayEnergy.class); +// register(TileEntityLaserRelayEnergyAdvanced.class); +// register(TileEntityLaserRelayEnergyExtreme.class); +// register(TileEntityLaserRelayItemWhitelist.class); +// register(TileEntityItemViewer.class); +// register(TileEntityDisplayStand.class); +// register(TileEntityShockSuppressor.class); +// register(TileEntityEmpowerer.class); +// register(TileEntityLaserRelayFluids.class); +// register(TileEntityBioReactor.class); +// register(TileEntityFarmer.class); +// register(TileEntityItemViewerHopping.class); +// register(TileEntityBatteryBox.class); +// } - register(TileEntityCompost.class); - register(TileEntityFeeder.class); - register(TileEntityGiantChest.class); - register(TileEntityGiantChestMedium.class); - register(TileEntityGiantChestLarge.class); - register(TileEntityGrinder.class); - register(TileEntityFurnaceDouble.class); - register(TileEntityInputter.class); - register(TileEntityFishingNet.class); - register(TileEntityFurnaceSolar.class); - register(TileEntityHeatCollector.class); - register(TileEntityItemRepairer.class); - register(TileEntityBreaker.class); - register(TileEntityDropper.class); - register(TileEntityInputterAdvanced.class); - register(TileEntityPlacer.class); - register(TileEntityGrinderDouble.class); - register(TileEntityCanolaPress.class); - register(TileEntityFermentingBarrel.class); - register(TileEntityOilGenerator.class); - register(TileEntityCoalGenerator.class); - register(TileEntityPhantomItemface.class); - register(TileEntityPhantomLiquiface.class); - register(TileEntityPhantomEnergyface.class); - register(TileEntityPlayerInterface.class); - register(TileEntityPhantomPlacer.class); - register(TileEntityPhantomBreaker.class); - register(TileEntityFluidCollector.class); - register(TileEntityFluidPlacer.class); - register(TileEntityLavaFactoryController.class); - register(TileEntityCoffeeMachine.class); - register(TileEntityPhantomBooster.class); - register(TileEntityEnergizer.class); - register(TileEntityEnervator.class); - register(TileEntityXPSolidifier.class); - register(TileEntitySmileyCloud.class); - register(TileEntityLeafGenerator.class); - register(TileEntityDirectionalBreaker.class); - register(TileEntityRangedCollector.class); - register(TileEntityAtomicReconstructor.class); - register(TileEntityMiner.class); - register(TileEntityFireworkBox.class); - register(TileEntityPhantomRedstoneface.class); - register(TileEntityLaserRelayItem.class); - register(TileEntityLaserRelayEnergy.class); - register(TileEntityLaserRelayEnergyAdvanced.class); - register(TileEntityLaserRelayEnergyExtreme.class); - register(TileEntityLaserRelayItemWhitelist.class); - register(TileEntityItemViewer.class); - register(TileEntityDisplayStand.class); - register(TileEntityShockSuppressor.class); - register(TileEntityEmpowerer.class); - register(TileEntityLaserRelayFluids.class); - register(TileEntityBioReactor.class); - register(TileEntityFarmer.class); - register(TileEntityItemViewerHopping.class); - register(TileEntityBatteryBox.class); - } +// private static void register(Class tileClass) { +// try { +// //This is hacky and dirty but it works so whatever +// ResourceLocation name = new ResourceLocation(ActuallyAdditions.MODID, tileClass.newInstance().name); +// GameRegistry.registerTileEntity(tileClass, name); +// } catch (Exception e) { +// ActuallyAdditions.LOGGER.fatal("Registering a TileEntity failed!", e); +// } +// } - private static void register(Class tileClass) { - try { - //This is hacky and dirty but it works so whatever - ResourceLocation name = new ResourceLocation(ActuallyAdditions.MODID, tileClass.newInstance().name); - GameRegistry.registerTileEntity(tileClass, name); - } catch (Exception e) { - ActuallyAdditions.LOGGER.fatal("Registering a TileEntity failed!", e); - } - } @Override - public final NBTTagCompound writeToNBT(NBTTagCompound compound) { + public CompoundNBT write(CompoundNBT compound) { this.writeSyncableNBT(compound, NBTType.SAVE_TILE); return compound; } @Override - public final void readFromNBT(NBTTagCompound compound) { + public void read(CompoundNBT compound) { this.readSyncableNBT(compound, NBTType.SAVE_TILE); } + @Nullable @Override - public final SPacketUpdateTileEntity getUpdatePacket() { - NBTTagCompound compound = new NBTTagCompound(); + public SUpdateTileEntityPacket getUpdatePacket() { + CompoundNBT compound = new CompoundNBT(); this.writeSyncableNBT(compound, NBTType.SYNC); - return new SPacketUpdateTileEntity(this.pos, -1, compound); + return new SUpdateTileEntityPacket(this.pos, -1, compound); } @Override - public final void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) { + public final void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) { this.readSyncableNBT(pkt.getNbtCompound(), NBTType.SYNC); } @Override - public final NBTTagCompound getUpdateTag() { - NBTTagCompound compound = new NBTTagCompound(); + public final CompoundNBT getUpdateTag() { + CompoundNBT compound = new CompoundNBT(); this.writeSyncableNBT(compound, NBTType.SYNC); return compound; } @Override - public final void handleUpdateTag(NBTTagCompound compound) { + public final void handleUpdateTag(CompoundNBT compound) { this.readSyncableNBT(compound, NBTType.SYNC); } public final void sendUpdate() { if (this.world != null && !this.world.isRemote) VanillaPacketDispatcher.dispatchTEToNearbyPlayers(this); /* + todo: this wan't me if(this.world != null && !this.world.isRemote){ - NBTTagCompound compound = new NBTTagCompound(); + CompoundNBT compound = new CompoundNBT(); this.writeSyncableNBT(compound, NBTType.SYNC); - NBTTagCompound data = new NBTTagCompound(); + CompoundNBT data = new CompoundNBT(); data.setTag("Data", compound); data.setInteger("X", this.pos.getX()); data.setInteger("Y", this.pos.getY()); @@ -165,25 +167,25 @@ public abstract class TileEntityBase extends TileEntity implements ITickable { } public void writeSyncableNBT(CompoundNBT compound, NBTType type) { - if (type != NBTType.SAVE_BLOCK) super.writeToNBT(compound); + if (type != NBTType.SAVE_BLOCK) super.write(compound); if (type == NBTType.SAVE_TILE) { - compound.setBoolean("Redstone", this.isRedstonePowered); - compound.setInteger("TicksElapsed", this.ticksElapsed); - compound.setBoolean("StopDrop", this.stopFromDropping); - } else if (type == NBTType.SYNC && this.stopFromDropping) compound.setBoolean("StopDrop", this.stopFromDropping); + compound.putBoolean("Redstone", this.isRedstonePowered); + compound.putInt("TicksElapsed", this.ticksElapsed); + compound.putBoolean("StopDrop", this.stopFromDropping); + } else if (type == NBTType.SYNC && this.stopFromDropping) compound.putBoolean("StopDrop", this.stopFromDropping); if (this.isRedstoneToggle() && (type != NBTType.SAVE_BLOCK || this.isPulseMode)) { - compound.setBoolean("IsPulseMode", this.isPulseMode); + compound.putBoolean("IsPulseMode", this.isPulseMode); } } public void readSyncableNBT(CompoundNBT compound, NBTType type) { - if (type != NBTType.SAVE_BLOCK) super.readFromNBT(compound); + if (type != NBTType.SAVE_BLOCK) super.read(compound); if (type == NBTType.SAVE_TILE) { this.isRedstonePowered = compound.getBoolean("Redstone"); - this.ticksElapsed = compound.getInteger("TicksElapsed"); + this.ticksElapsed = compound.getInt("TicksElapsed"); this.stopFromDropping = compound.getBoolean("StopDrop"); } else if (type == NBTType.SYNC) this.stopFromDropping = compound.getBoolean("StopDrop"); @@ -192,22 +194,18 @@ public abstract class TileEntityBase extends TileEntity implements ITickable { } } - @Override - public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) { - return !oldState.getBlock().isAssociatedBlock(newState.getBlock()); - } +// todo: re-eval +// @Override +// public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState) { +// return !oldState.getBlock().isAssociatedBlock(newState.getBlock()); +// } public String getNameForTranslation() { return "container." + ActuallyAdditions.MODID + "." + this.name + ".name"; } @Override - public ITextComponent getDisplayName() { - return new TextComponentTranslation(this.getNameForTranslation()); - } - - @Override - public final void update() { + public void tick() { this.updateEntity(); } @@ -227,14 +225,14 @@ public abstract class TileEntityBase extends TileEntity implements ITickable { if (provider.doesShareEnergy()) { int total = provider.getEnergyToSplitShare(); if (total > 0) { - EnumFacing[] sides = provider.getEnergyShareSides(); + Direction[] sides = provider.getEnergyShareSides(); int amount = total / sides.length; if (amount <= 0) { amount = total; } - for (EnumFacing side : sides) { + for (Direction side : sides) { TileEntity tile = this.tilesAround[side.ordinal()]; if (tile != null && provider.canShareTo(tile)) { WorldUtil.doEnergyInteraction(this, tile, side, amount); @@ -249,14 +247,14 @@ public abstract class TileEntityBase extends TileEntity implements ITickable { if (handler.doesShareFluid()) { int total = handler.getMaxFluidAmountToSplitShare(); if (total > 0) { - EnumFacing[] sides = handler.getFluidShareSides(); + Direction[] sides = handler.getFluidShareSides(); int amount = total / sides.length; if (amount <= 0) { amount = total; } - for (EnumFacing side : sides) { + for (Direction side : sides) { TileEntity tile = this.tilesAround[side.ordinal()]; if (tile != null) { WorldUtil.doFluidInteraction(this, tile, side, amount); @@ -277,7 +275,7 @@ public abstract class TileEntityBase extends TileEntity implements ITickable { } public void saveDataOnChangeOrWorldStart() { - for (EnumFacing side : EnumFacing.values()) { + for (Direction side : Direction.values()) { BlockPos pos = this.pos.offset(side); if (this.world.isBlockLoaded(pos)) { this.tilesAround[side.ordinal()] = this.world.getTileEntity(pos); @@ -294,8 +292,8 @@ public abstract class TileEntityBase extends TileEntity implements ITickable { this.markDirty(); } - public boolean canPlayerUse(EntityPlayer player) { - return player.getDistanceSq(this.getPos().getX() + 0.5D, this.pos.getY() + 0.5D, this.pos.getZ() + 0.5D) <= 64 && !this.isInvalid() && this.world.getTileEntity(this.pos) == this; + public boolean canPlayerUse(PlayerEntity player) { + return player.getDistanceSq(this.getPos().getX() + 0.5D, this.pos.getY() + 0.5D, this.pos.getZ() + 0.5D) <= 64 && !this.isRemoved() && this.world.getTileEntity(this.pos) == this; } protected boolean sendUpdateWithInterval() { @@ -307,36 +305,33 @@ public abstract class TileEntityBase extends TileEntity implements ITickable { } } + @Nonnull @Override - public boolean hasCapability(Capability capability, EnumFacing facing) { - return this.getCapability(capability, facing) != null; - } - - @SuppressWarnings("unchecked") - @Override - public T getCapability(Capability capability, EnumFacing facing) { - if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { - IItemHandler handler = this.getItemHandler(facing); - if (handler != null) { return (T) handler; } - } else if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) { - IFluidHandler tank = this.getFluidHandler(facing); - if (tank != null) { return (T) tank; } - } else if (capability == CapabilityEnergy.ENERGY) { - IEnergyStorage storage = this.getEnergyStorage(facing); - if (storage != null) { return (T) storage; } + public LazyOptional getCapability(@Nonnull Capability cap, @Nullable Direction side) { + if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) { + return this.getItemHandler(side).cast(); } - return super.getCapability(capability, facing); + + if (cap == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY) { + return this.getFluidHandler(side).cast(); + } + + if (cap == CapabilityEnergy.ENERGY) { + return this.getEnergyStorage(side).cast(); + } + + return super.getCapability(cap, side); } - public IFluidHandler getFluidHandler(EnumFacing facing) { + public LazyOptional getFluidHandler(Direction facing) { return null; } - public IEnergyStorage getEnergyStorage(EnumFacing facing) { + public LazyOptional getEnergyStorage(Direction facing) { return null; } - public IItemHandler getItemHandler(EnumFacing facing) { + public LazyOptional getItemHandler(Direction facing) { return null; } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityInventoryBase.java b/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityInventoryBase.java index 7a5f7ffc9..9279704aa 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityInventoryBase.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/tile/TileEntityInventoryBase.java @@ -5,49 +5,54 @@ import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA.IAcceptor; import de.ellpeck.actuallyadditions.common.util.ItemStackHandlerAA.IRemover; import de.ellpeck.actuallyadditions.common.util.StackUtil; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraft.util.EnumFacing; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.nbt.ListNBT; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.tileentity.TileEntityType; +import net.minecraft.util.Direction; +import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.items.ItemHandlerHelper; -public abstract class TileEntityInventoryBase extends TileEntityBase { +public abstract class TileEntityInventoryBase extends TileEntityBase { public final ItemStackHandlerAA inv; + private final LazyOptional lazyInv; - public TileEntityInventoryBase(int slots, String name) { - super(name); + public TileEntityInventoryBase(TileEntityType type, int slots, String name) { + super(type, name); this.inv = new TileStackHandler(slots); + this.lazyInv = LazyOptional.of(() -> this.inv); } - public static void saveSlots(IItemHandler slots, NBTTagCompound compound) { + public static void saveSlots(IItemHandler slots, CompoundNBT compound) { if (slots != null && slots.getSlots() > 0) { - NBTTagList tagList = new NBTTagList(); + ListNBT tagList = new ListNBT(); for (int i = 0; i < slots.getSlots(); i++) { ItemStack slot = slots.getStackInSlot(i); - NBTTagCompound tagCompound = new NBTTagCompound(); + CompoundNBT tagCompound = new CompoundNBT(); if (StackUtil.isValid(slot)) { - slot.writeToNBT(tagCompound); + slot.write(tagCompound); } - tagList.appendTag(tagCompound); + tagList.add(tagCompound); } - compound.setTag("Items", tagList); + compound.put("Items", tagList); } } - public static void loadSlots(IItemHandlerModifiable slots, NBTTagCompound compound) { + public static void loadSlots(IItemHandlerModifiable slots, CompoundNBT compound) { if (slots != null && slots.getSlots() > 0) { - NBTTagList tagList = compound.getTagList("Items", 10); + ListNBT tagList = compound.getList("Items", 10); for (int i = 0; i < slots.getSlots(); i++) { - NBTTagCompound tagCompound = tagList.getCompoundTagAt(i); - slots.setStackInSlot(i, tagCompound != null && tagCompound.hasKey("id") ? new ItemStack(tagCompound) : StackUtil.getEmpty()); + CompoundNBT tagCompound = tagList.getCompound(i); + slots.setStackInSlot(i, tagCompound.contains("id") ? ItemStack.read(tagCompound) : StackUtil.getEmpty()); } } } @Override - public void writeSyncableNBT(NBTTagCompound compound, NBTType type) { + public void writeSyncableNBT(CompoundNBT compound, NBTType type) { super.writeSyncableNBT(compound, type); if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) { saveSlots(this.inv, compound); @@ -55,8 +60,8 @@ public abstract class TileEntityInventoryBase extends TileEntityBase { } @Override - public IItemHandler getItemHandler(EnumFacing facing) { - return this.inv; + public LazyOptional getItemHandler(Direction facing) { + return this.lazyInv; } public IAcceptor getAcceptor() { @@ -90,7 +95,7 @@ public abstract class TileEntityInventoryBase extends TileEntityBase { } @Override - public void readSyncableNBT(NBTTagCompound compound, NBTType type) { + public void readSyncableNBT(CompoundNBT compound, NBTType type) { super.readSyncableNBT(compound, type); if (type == NBTType.SAVE_TILE || type == NBTType.SYNC && this.shouldSyncSlots()) { loadSlots(this.inv, compound); diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/util/WorldUtil.java b/src/main/java/de/ellpeck/actuallyadditions/common/util/WorldUtil.java index 0c0fa34e4..921c67192 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/util/WorldUtil.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/util/WorldUtil.java @@ -1,33 +1,25 @@ package de.ellpeck.actuallyadditions.common.util; -import java.util.ArrayList; - -import org.cyclops.commoncapabilities.api.capability.itemhandler.ISlotlessItemHandler; - import de.ellpeck.actuallyadditions.common.ActuallyAdditions; import de.ellpeck.actuallyadditions.common.tile.FilterSettings; import de.ellpeck.actuallyadditions.common.util.compat.SlotlessableItemHandlerWrapper; import net.minecraft.block.Block; -import net.minecraft.block.BlockDirectional; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; import net.minecraft.block.material.Material; -import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.item.EntityItem; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.init.Blocks; -import net.minecraft.init.Items; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.ItemStack; -import net.minecraft.network.play.server.SPacketBlockChange; +import net.minecraft.item.Items; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; +import net.minecraft.util.Direction; +import net.minecraft.util.Hand; import net.minecraft.util.NonNullList; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; -import net.minecraft.world.WorldServer; import net.minecraftforge.common.ForgeHooks; import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.MinecraftForge; @@ -42,6 +34,8 @@ import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.items.IItemHandler; +import java.util.ArrayList; + public final class WorldUtil { public static boolean doItemInteraction(SlotlessableItemHandlerWrapper extractWrapper, SlotlessableItemHandlerWrapper insertWrapper, int maxExtract) { @@ -108,9 +102,9 @@ public final class WorldUtil { return extracted; } - public static void doEnergyInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing sideTo, int maxTransfer) { + public static void doEnergyInteraction(TileEntity tileFrom, TileEntity tileTo, Direction sideTo, int maxTransfer) { if (maxTransfer > 0) { - EnumFacing opp = sideTo == null ? null : sideTo.getOpposite(); + Direction opp = sideTo == null ? null : sideTo.getOpposite(); IEnergyStorage handlerFrom = tileFrom.getCapability(CapabilityEnergy.ENERGY, sideTo); IEnergyStorage handlerTo = tileTo.getCapability(CapabilityEnergy.ENERGY, opp); if (handlerFrom != null && handlerTo != null) { @@ -124,7 +118,7 @@ public final class WorldUtil { } } - public static void doFluidInteraction(TileEntity tileFrom, TileEntity tileTo, EnumFacing sideTo, int maxTransfer) { + public static void doFluidInteraction(TileEntity tileFrom, TileEntity tileTo, Direction sideTo, int maxTransfer) { if (maxTransfer > 0) { if (tileFrom.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo) && tileTo.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo.getOpposite())) { IFluidHandler handlerFrom = tileFrom.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, sideTo); @@ -155,7 +149,7 @@ public final class WorldUtil { return true; } - public static ItemStack useItemAtSide(EnumFacing side, World world, BlockPos pos, ItemStack stack) { + public static ItemStack useItemAtSide(Direction side, World world, BlockPos pos, ItemStack stack) { if (world instanceof WorldServer && StackUtil.isValid(stack) && pos != null) { BlockPos offsetPos = pos.offset(side); IBlockState state = world.getBlockState(offsetPos); @@ -192,7 +186,7 @@ public final class WorldUtil { return stack; } - public static boolean dropItemAtSide(EnumFacing side, World world, BlockPos pos, ItemStack stack) { + public static boolean dropItemAtSide(Direction side, World world, BlockPos pos, ItemStack stack) { BlockPos coords = pos.offset(side); if (world.isBlockLoaded(coords)) { EntityItem item = new EntityItem(world, coords.getX() + 0.5, coords.getY() + 0.5, coords.getZ() + 0.5, stack); @@ -205,33 +199,33 @@ public final class WorldUtil { return false; } - public static EnumFacing getDirectionBySidesInOrder(int side) { + public static Direction getDirectionBySidesInOrder(int side) { switch (side) { case 0: - return EnumFacing.UP; + return Direction.UP; case 1: - return EnumFacing.DOWN; + return Direction.DOWN; case 2: - return EnumFacing.NORTH; + return Direction.NORTH; case 3: - return EnumFacing.EAST; + return Direction.EAST; case 4: - return EnumFacing.SOUTH; + return Direction.SOUTH; default: - return EnumFacing.WEST; + return Direction.WEST; } } - public static EnumFacing getDirectionByPistonRotation(IBlockState state) { + public static Direction getDirectionByPistonRotation(IBlockState state) { return state.getValue(BlockDirectional.FACING); } public static ArrayList getMaterialsAround(World world, BlockPos pos) { ArrayList blocks = new ArrayList<>(); - blocks.add(world.getBlockState(pos.offset(EnumFacing.NORTH)).getMaterial()); - blocks.add(world.getBlockState(pos.offset(EnumFacing.EAST)).getMaterial()); - blocks.add(world.getBlockState(pos.offset(EnumFacing.SOUTH)).getMaterial()); - blocks.add(world.getBlockState(pos.offset(EnumFacing.WEST)).getMaterial()); + blocks.add(world.getBlockState(pos.offset(Direction.NORTH)).getMaterial()); + blocks.add(world.getBlockState(pos.offset(Direction.EAST)).getMaterial()); + blocks.add(world.getBlockState(pos.offset(Direction.SOUTH)).getMaterial()); + blocks.add(world.getBlockState(pos.offset(Direction.WEST)).getMaterial()); return blocks; } @@ -256,7 +250,7 @@ public final class WorldUtil { return world.rayTraceBlocks(vec3, vec31, p1, p2, p3); } - public static RayTraceResult getNearestBlockWithDefaultReachDistance(World world, EntityPlayer player) { + public static RayTraceResult getNearestBlockWithDefaultReachDistance(World world, PlayerEntity player) { return getNearestBlockWithDefaultReachDistance(world, player, false, true, false); } @@ -264,10 +258,10 @@ public final class WorldUtil { return getMovingObjectPosWithReachDistance(world, player, player.getEntityAttribute(EntityPlayer.REACH_DISTANCE).getAttributeValue(), stopOnLiquids, ignoreBlockWithoutBoundingBox, returnLastUncollidableBlock); } - public static void setHandItemWithoutAnnoyingSound(EntityPlayer player, EnumHand hand, ItemStack stack) { - if (hand == EnumHand.MAIN_HAND) { + public static void setHandItemWithoutAnnoyingSound(PlayerEntity player, Hand hand, ItemStack stack) { + if (hand == Hand.MAIN_HAND) { player.inventory.mainInventory.set(player.inventory.currentItem, stack); - } else if (hand == EnumHand.OFF_HAND) { + } else if (hand == Hand.OFF_HAND) { player.inventory.offHandInventory.set(0, stack); } } @@ -294,18 +288,18 @@ public final class WorldUtil { * @param pos The pos to break. * @return If the break was successful. */ - public static boolean breakExtraBlock(ItemStack stack, World world, EntityPlayer player, BlockPos pos) { - IBlockState state = world.getBlockState(pos); + public static boolean breakExtraBlock(ItemStack stack, World world, PlayerEntity player, BlockPos pos) { + BlockState state = world.getBlockState(pos); Block block = state.getBlock(); - if (player.capabilities.isCreativeMode) { - if (block.removedByPlayer(state, world, pos, player, false)) { + if (player.isCreative()) { + if (block.removedByPlayer(state, world, pos, player, false, world.getFluidState(pos))) { block.onPlayerDestroy(world, pos, state); } // send update to client if (!world.isRemote) { - ((EntityPlayerMP) player).connection.sendPacket(new SPacketBlockChange(world, pos)); + ((ServerPlayerEntity) player).connection.sendPacket(new SPacketBlockChange(world, pos)); } return true; } @@ -316,18 +310,18 @@ public final class WorldUtil { // server sided handling if (!world.isRemote) { // send the blockbreak event - int xp = ForgeHooks.onBlockBreakEvent(world, ((EntityPlayerMP) player).interactionManager.getGameType(), (EntityPlayerMP) player, pos); + int xp = ForgeHooks.onBlockBreakEvent(world, ((ServerPlayerEntity) player).interactionManager.getGameType(), (ServerPlayerEntity) player, pos); if (xp == -1) return false; TileEntity tileEntity = world.getTileEntity(pos); - if (block.removedByPlayer(state, world, pos, player, true)) { // boolean is if block can be harvested, checked above + if (block.removedByPlayer(state, world, pos, player, true, world.getFluidState(pos))) { // boolean is if block can be harvested, checked above block.onPlayerDestroy(world, pos, state); block.harvestBlock(world, player, pos, state, tileEntity, stack); block.dropXpOnBlockBreak(world, pos, xp); } // always send block update to client - ((EntityPlayerMP) player).connection.sendPacket(new SPacketBlockChange(world, pos)); + ((ServerPlayerEntity) player).connection.sendPacket(new SPacketBlockChange(world, pos)); return true; } // client sided handling @@ -337,7 +331,7 @@ public final class WorldUtil { // following code can be found in PlayerControllerMP.onPlayerDestroyBlock world.playEvent(2001, pos, Block.getStateId(state)); - if (block.removedByPlayer(state, world, pos, player, true)) { + if (block.removedByPlayer(state, world, pos, player, true, world.getFluidState(pos))) { block.onPlayerDestroy(world, pos, state); } // callback to the tool