From c9b42b911c3b79f11cb52fd5a41f6a83828c50c7 Mon Sep 17 00:00:00 2001 From: Tanvir Talukder Date: Sat, 25 Feb 2017 13:40:25 -0600 Subject: [PATCH 01/15] Untrack log files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 30adbac..6134362 100644 --- a/.gitignore +++ b/.gitignore @@ -70,3 +70,4 @@ gradle.properties gradle/ gradlew gradlew.bat +logs/ From e8d0e9fe0f82f75a3a1f1767ba3a4541f7b124c4 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 12 Mar 2017 17:57:16 -0500 Subject: [PATCH 02/15] Created all necessary classes for grinder. Broken --- .../minecraft/mod/CustomMod.java | 4 + .../minecraft/mod/blocks/ModBlocks.java | 21 +- .../mod/blocks/grinder/BlockGrinder.java | 280 +++++++++++ .../mod/blocks/grinder/ContainerGrinder.java | 187 ++++++++ .../mod/blocks/grinder/GrinderRecipes.java | 101 ++++ .../mod/blocks/grinder/SlotGrinderOutput.java | 99 ++++ .../mod/blocks/grinder/TileEntityGrinder.java | 451 ++++++++++++++++++ .../minecraft/mod/guis/GuiGrinder.java | 80 ++++ .../minecraft/mod/guis/GuiHandler.java | 39 ++ .../minecraft/mod/proxy/CommonProxy.java | 6 + .../BlockContainerTileEntity.java | 40 ++ .../mod/tileentities/BlockTileEntity.java | 2 +- .../assets/custommod/blockstates/grinder.json | 9 + .../assets/custommod/lang/en_US.lang | 2 + .../custommod/models/block/grinder.json | 9 + .../assets/custommod/models/item/grinder.json | 10 + .../textures/gui/container/grinder.png | Bin 0 -> 1416 bytes 17 files changed, 1335 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java create mode 100644 src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java create mode 100644 src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java create mode 100644 src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java create mode 100644 src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java create mode 100644 src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java create mode 100644 src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java create mode 100644 src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java create mode 100644 src/main/resources/assets/custommod/blockstates/grinder.json create mode 100644 src/main/resources/assets/custommod/models/block/grinder.json create mode 100644 src/main/resources/assets/custommod/models/item/grinder.json create mode 100644 src/main/resources/assets/custommod/textures/gui/container/grinder.png diff --git a/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java b/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java index 5bf2766..1b2cd7b 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java @@ -24,6 +24,10 @@ public class CustomMod { public static final CustomTab tab = new CustomTab(); + public enum GUI_ENUM { + GRINDER + } + @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { proxy.preInit(event); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java index 7512686..ec52c18 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java @@ -1,13 +1,18 @@ package com.quantumindustries.minecraft.mod.blocks; +import com.quantumindustries.minecraft.mod.CustomMod; import com.quantumindustries.minecraft.mod.ItemModelProvider; -import com.quantumindustries.minecraft.mod.blocks.counter.BlockCounter; +import com.quantumindustries.minecraft.mod.blocks.grinder.BlockGrinder; import com.quantumindustries.minecraft.mod.blocks.infiniteproducer.BlockInfiniteProducer; import com.quantumindustries.minecraft.mod.blocks.poweranalyzer.BlockPowerAnalyzer; import com.quantumindustries.minecraft.mod.items.ItemOreDict; +import com.quantumindustries.minecraft.mod.tileentities.BlockContainerTileEntity; import com.quantumindustries.minecraft.mod.tileentities.BlockTileEntity; import net.minecraft.block.Block; -import net.minecraft.block.material.Material; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.RenderItem; +import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraftforge.fml.common.registry.GameRegistry; @@ -19,12 +24,15 @@ public class ModBlocks { public static BlockOre blockCobalt; public static BlockOre blockRhodium; - // TODO(CM): Separate out different registers into functions (register ores, blocks, etc.) + public static BlockGrinder blockGrinder; + public static void init() { initOres(); initOreBlocks(); register(new BlockInfiniteProducer()); register(new BlockPowerAnalyzer()); + + initBlockGrinder(); } private static void initOres() { @@ -37,6 +45,10 @@ private static void initOreBlocks() { blockRhodium = register(new BlockOre("blockRhodium", "blockRhodium", 3f, 5f)); } + private static void initBlockGrinder() { + blockGrinder = register(new BlockGrinder()); + } + // Registers blocks and checks what they are instanceof // for further registrations. private static T register(T block, ItemBlock itemBlock) { @@ -49,7 +61,8 @@ private static T register(T block, ItemBlock itemBlock) { ((ItemModelProvider) block).registerItemModel(itemBlock); } - if(block instanceof BlockTileEntity) { + if(block instanceof BlockTileEntity || + block instanceof BlockContainerTileEntity) { GameRegistry.registerTileEntity( ((BlockTileEntity) block).getTileEntityClass(), block.getRegistryName().toString() diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java new file mode 100644 index 0000000..d22714c --- /dev/null +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java @@ -0,0 +1,280 @@ +package com.quantumindustries.minecraft.mod.blocks.grinder; + +import java.util.Random; + +import com.quantumindustries.minecraft.mod.CustomMod; +import com.quantumindustries.minecraft.mod.blocks.ModBlocks; +import com.quantumindustries.minecraft.mod.tileentities.BlockContainerTileEntity; +import net.minecraft.block.SoundType; +import net.minecraft.block.material.Material; +import net.minecraft.block.properties.IProperty; +import net.minecraft.block.properties.PropertyDirection; +import net.minecraft.block.state.BlockStateContainer; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.RenderItem; +import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.InventoryHelper; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumBlockRenderType; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.world.World; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import javax.annotation.Nullable; + +public class BlockGrinder extends BlockContainerTileEntity { + + public static final PropertyDirection FACING = PropertyDirection.create( + "facing", EnumFacing.Plane.HORIZONTAL + ); + private final boolean isGrinding; + private boolean hasTileEntity; + + public BlockGrinder() { + super(Material.ROCK, "blockGrinder"); + setDefaultState(blockState.getBaseState().withProperty( + FACING, + EnumFacing.NORTH + )); + isGrinding = true; + hasTileEntity = false; + blockSoundType = SoundType.SNOW; + blockParticleGravity = 1.0f; + slipperiness = 0.6f; + lightOpacity = 20; // cast a light shadow + setTickRandomly(false); + useNeighborBrightness = false; + } + + @Override + public Item getItemDropped(IBlockState state, Random rand, int fortune) { + return Item.getItemFromBlock(ModBlocks.blockGrinder); + } + + @Override + public void onBlockAdded(World parWorld, BlockPos parBlockPos, IBlockState parIBlockState) { + if(!parWorld.isRemote) { + EnumFacing enumFacing = getUnblockedFace( + parWorld, + parBlockPos, + parIBlockState + ); + + parWorld.setBlockState( + parBlockPos, + parIBlockState.withProperty(FACING, enumFacing), + 2 + ); + } + } + + @Override + public boolean onBlockActivated(World parWorld, BlockPos parBlockPos, + IBlockState parIBlockState, EntityPlayer parPlayer, + EnumHand parHand, ItemStack parStack, EnumFacing parFacing, + float hitX, float hitY, float hitZ) { + if(!parWorld.isRemote) { + parPlayer.openGui( + CustomMod.instance, + CustomMod.GUI_ENUM.GRINDER.ordinal(), + parWorld, + parBlockPos.getX(), + parBlockPos.getY(), + parBlockPos.getZ() + ); + } + + return true; + } + + @Override + public TileEntity createNewTileEntity(World worldIn, int meta) { + return new TileEntityGrinder(); + } + + @Override + public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, + float hitX, float hitY, float hitZ, int meta, + EntityLivingBase placer, ItemStack stack) { + return getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite()); + } + + @Override + public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, + EntityLivingBase placer, ItemStack stack) { + worldIn.setBlockState(pos, state.withProperty( + FACING, + placer.getHorizontalFacing().getOpposite()), + 2 + ); + + if(stack.hasDisplayName()) { + TileEntity tileEntity = worldIn.getTileEntity(pos); + + if(tileEntity instanceof TileEntityGrinder) { + ((TileEntityGrinder) tileEntity).setCustomInventoryName(stack.getDisplayName()); + } + } + } + + @Override + public void breakBlock(World worldIn, BlockPos pos, IBlockState state) { + if(!hasTileEntity) { + TileEntity tileEntity = worldIn.getTileEntity(pos); + + if(tileEntity instanceof TileEntityGrinder) { + InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityGrinder) tileEntity); + worldIn.updateComparatorOutputLevel(pos, this); + } + } + + super.breakBlock(worldIn, pos, state); + } + + @Override + @SideOnly(Side.CLIENT) + public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, + BlockPos pos, EntityPlayer player) { + return new ItemStack(Item.getItemFromBlock(ModBlocks.blockGrinder)); + } + + @Override + public EnumBlockRenderType getRenderType(IBlockState parIBlockState) { + return EnumBlockRenderType.MODEL; + } + + @SuppressWarnings("deprecation") + @Override + public IBlockState getStateFromMeta(int meta) { + EnumFacing enumFacing = EnumFacing.getFront(meta); + + if(enumFacing.getAxis() == EnumFacing.Axis.Y) { + enumFacing = EnumFacing.NORTH; + } + + return getDefaultState().withProperty(FACING, enumFacing); + } + + @Override + public int getMetaFromState(IBlockState state) { + return state.getValue(FACING).getIndex(); + } + + public boolean isGrinding() { + return isGrinding; + } + + @Override + public Class getTileEntityClass() { + return TileEntityGrinder.class; + } + + @Nullable + @Override + public TileEntityGrinder createTileEntity(World world, IBlockState state) { + return new TileEntityGrinder(); + } + + @Override + public void registerItemModel(Item item) { + RenderItem renderItem = Minecraft.getMinecraft().getRenderItem(); + renderItem.getItemModelMesher().register( + Item.getItemFromBlock(this), + 0, + new ModelResourceLocation( + CustomMod.MODID + ":" + getUnlocalizedName(), + "inventory" + ) + ); + } + + @Override + protected BlockStateContainer createBlockState() { + return new BlockStateContainer(this, new IProperty[] { FACING }); + } + + private EnumFacing getUnblockedFace(World parWorld, BlockPos parBlockPos, IBlockState parIBlockState) { + IBlockState blockToNorth = parWorld.getBlockState(parBlockPos.north()); + IBlockState blockToSouth = parWorld.getBlockState(parBlockPos.south()); + IBlockState blockToWest = parWorld.getBlockState(parBlockPos.west()); + IBlockState blockToEast = parWorld.getBlockState(parBlockPos.east()); + + EnumFacing enumFacing = parIBlockState.getValue(FACING); + + if(shouldFaceSouth(blockToNorth, blockToSouth, enumFacing)) { + enumFacing = EnumFacing.SOUTH; + } + else if(shouldFaceNorth(blockToNorth, blockToSouth, enumFacing)) { + enumFacing = EnumFacing.NORTH; + } + else if(shouldFaceEast(blockToWest, blockToEast, enumFacing)) { + enumFacing = EnumFacing.EAST; + } + else if(shouldFaceWest(blockToWest, blockToEast, enumFacing)) { + enumFacing = EnumFacing.WEST; + } + + return enumFacing; + } + + private boolean shouldFaceSouth(IBlockState blockToNorth, IBlockState blockToSouth, EnumFacing enumFacing) { + return enumFacing == EnumFacing.NORTH && + blockToNorth.isFullBlock() && + !blockToSouth.isFullBlock(); + } + + private boolean shouldFaceNorth(IBlockState blockToNorth, IBlockState blockToSouth, EnumFacing enumFacing) { + return enumFacing == EnumFacing.SOUTH && + blockToSouth.isFullBlock() && + !blockToNorth.isFullBlock(); + } + + private boolean shouldFaceEast(IBlockState blockToWest, IBlockState blockToEast, EnumFacing enumFacing) { + return enumFacing == EnumFacing.WEST && + blockToWest.isFullBlock() && + !blockToEast.isFullBlock(); + } + + private boolean shouldFaceWest(IBlockState blockToWest, IBlockState blockToEast, EnumFacing enumFacing) { + return enumFacing == EnumFacing.EAST && + blockToEast.isFullBlock() && + !blockToWest.isFullBlock(); + } + + @SideOnly(Side.CLIENT) + static final class SwitchEnumFacing { + static final int[] enumFacingArray = new int[EnumFacing.values().length]; + + static { + try { + enumFacingArray[EnumFacing.WEST.ordinal()] = 1; + } + catch(NoSuchFieldError e) {} + + try { + enumFacingArray[EnumFacing.EAST.ordinal()] = 2; + } + catch(NoSuchFieldError e) {} + + try { + enumFacingArray[EnumFacing.NORTH.ordinal()] = 3; + } + catch(NoSuchFieldError e) {} + + try { + enumFacingArray[EnumFacing.SOUTH.ordinal()] = 4; + } + catch(NoSuchFieldError e) {} + } + } + +} diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java new file mode 100644 index 0000000..a77ed92 --- /dev/null +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -0,0 +1,187 @@ +package com.quantumindustries.minecraft.mod.blocks.grinder; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.IContainerListener; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +public class ContainerGrinder extends Container { + + private final IInventory tileGrinder; + private final int sizeInventory; + private int ticksGrindingItemSoFar; + private int ticksPerItem; + private int timeCanGrind; + + public ContainerGrinder(InventoryPlayer parInventoryPlayer, IInventory parIInventory) { + tileGrinder = parIInventory; + sizeInventory = tileGrinder.getSizeInventory(); + addSlotToContainer( + new Slot( + tileGrinder, + TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal(), + 56, + 35 + ) + ); + addSlotToContainer( + new SlotGrinderOutput( + parInventoryPlayer.player, + tileGrinder, + TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal(), + 116, + 35 + ) + ); + + // add player inventory slots + // note that the slot numbers are within the player inventory so can be same as the tile entity inventory + for(int i = 0; i < 3; ++i) { + for(int j = 0; j < 9; ++j) { + addSlotToContainer( + new Slot( + parInventoryPlayer, + j + i * 9 + 9, + 8 + j * 18, + 84 + i * 18 + ) + ); + } + } + + // add hotbar slots + for(int i = 0; i < 9; ++i) { + addSlotToContainer( + new Slot( + parInventoryPlayer, + i, + 8 + i * 18, + 142 + ) + ); + } + } + + /** + * Looks for changes made in the container, sends them to every listener. + */ + @Override + public void detectAndSendChanges() { + super.detectAndSendChanges(); + + for(int i = 0; i < listeners.size(); ++i) { + IContainerListener listener = listeners.get(i); + + if(ticksGrindingItemSoFar != tileGrinder.getField(2)) { + listener.sendProgressBarUpdate(this, 2, tileGrinder.getField(2)); + } + + if(timeCanGrind != tileGrinder.getField(0)) { + listener.sendProgressBarUpdate(this, 0, tileGrinder.getField(0)); + } + + if(ticksPerItem != tileGrinder.getField(3)) { + listener.sendProgressBarUpdate(this, 3, tileGrinder.getField(3)); + } + } + + ticksGrindingItemSoFar = tileGrinder.getField(2); // tick grinding item so far + timeCanGrind = tileGrinder.getField(0); // time can grind + ticksPerItem = tileGrinder.getField(3); // ticks per item + } + + @Override + @SideOnly(Side.CLIENT) + public void updateProgressBar(int id, int data) { + tileGrinder.setField(id, data); + } + + @Override + public boolean canInteractWith(EntityPlayer playerIn) { + return tileGrinder.isUseableByPlayer(playerIn); + } + + /** + * Take a stack from the specified inventory slot. + */ + @Override + public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) { + ItemStack itemStack1 = null; + Slot slot = inventorySlots.get(slotIndex); + + if(slot != null && slot.getHasStack()) { + ItemStack itemStack2 = slot.getStack(); + itemStack1 = itemStack2.copy(); + + if(slotIndex == TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal()) { + if(!mergeItemStack( + itemStack2, + sizeInventory, + sizeInventory + 36, + true + )) { + return null; + } + + slot.onSlotChange(itemStack2, itemStack1); + } + else if(slotIndex != TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal()) { + // check if there is a grinding recipe for the stack + if(GrinderRecipes.instance().getGrindingResult(itemStack2) != null) { + if(!mergeItemStack(itemStack2, 0, 1, false)) { + return null; + } + } + else if(slotIndex >= sizeInventory && slotIndex < sizeInventory + 27) { // player inventory slots + if(!mergeItemStack( + itemStack2, + sizeInventory + 27, + sizeInventory + 36, + false + )) { + return null; + } + } + else if(slotIndex >= sizeInventory + 27 && + slotIndex < sizeInventory + 36 && + !mergeItemStack( + itemStack2, + sizeInventory + 1, + sizeInventory + 27, + false + )) { // hotbar slots + return null; + } + } + else if(!mergeItemStack( + itemStack2, + sizeInventory, + sizeInventory + 36, + false + )) { + return null; + } + + if(itemStack2.stackSize == 0) { + slot.putStack(null); + } + else { + slot.onSlotChanged(); + } + + if(itemStack2.stackSize == itemStack1.stackSize) { + return null; + } + + slot.onPickupFromSlot(playerIn, itemStack2); + } + + return itemStack1; + } + +} diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java new file mode 100644 index 0000000..718f8e4 --- /dev/null +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java @@ -0,0 +1,101 @@ +package com.quantumindustries.minecraft.mod.blocks.grinder; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import com.google.common.collect.Maps; + +import net.minecraft.init.Blocks; +import net.minecraft.init.Items; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; + +public class GrinderRecipes { + + private static final GrinderRecipes grindingBase = new GrinderRecipes(); + /** The list of grinding results. */ + private final Map grindingList = Maps.newHashMap(); + private final Map experienceList = Maps.newHashMap(); + + public static GrinderRecipes instance() { + return grindingBase; + } + + private GrinderRecipes() { + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.STONEBRICK), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.STONE_SLAB), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.STONE_SLAB2), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SANDSTONE_STAIRS), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.STONE), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.SAND)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SANDSTONE), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.SAND)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.GLASS), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.SAND)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.BRICK_BLOCK), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.PLANKS), 1, 32767), new ItemStack(Items.PAPER, 10), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.LOG), 1, 32767), new ItemStack(Items.PAPER), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.LOG2), 1, 32767), new ItemStack(Items.PAPER), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.NETHER_BRICK), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.NETHERRACK)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.NETHER_BRICK_STAIRS), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.NETHERRACK)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.NETHER_BRICK_FENCE), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.NETHERRACK)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.NETHERRACK), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.SOUL_SAND)), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SOUL_SAND), 1, 32767), new ItemStack(Items.GUNPOWDER, 4), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SLIME_BLOCK), 1, 32767), new ItemStack(Items.SLIME_BALL, 9), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.OBSIDIAN), 1, 32767), new ItemStack(Items.FLINT, 10), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.PRISMARINE), 1, 32767), new ItemStack(Items.PRISMARINE_SHARD, 10), 0.7f); + addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SEA_LANTERN), 1, 32767), new ItemStack(Items.PRISMARINE_CRYSTALS, 9), 0.7f); + } + + public void addGrindingRecipe(ItemStack parItemStackIn, ItemStack parItemStackOut, + float parExperience) { + grindingList.put(parItemStackIn, parItemStackOut); + experienceList.put(parItemStackOut, Float.valueOf(parExperience)); + } + + /** + * Returns the grinding result of an item. + */ + public ItemStack getGrindingResult(ItemStack parItemStack) { + Iterator iterator = grindingList.entrySet().iterator(); + Entry entry; + + do { + if(!iterator.hasNext()) { + return null; + } + + entry = (Entry) iterator.next(); + } + while(!areItemStacksEqual(parItemStack, (ItemStack)entry.getKey())); + + return (ItemStack) entry.getValue(); + } + + private boolean areItemStacksEqual(ItemStack parItemStack1, ItemStack parItemStack2) { + return parItemStack2.getItem() == parItemStack1.getItem() && + (parItemStack2.getMetadata() == 32767 || + parItemStack2.getMetadata() == parItemStack1.getMetadata() + ); + } + + public Map getGrindingList() { + return grindingList; + } + + public float getGrindingExperience(ItemStack parItemStack) { + Iterator iterator = experienceList.entrySet().iterator(); + Entry entry; + + do { + if(!iterator.hasNext()) { + return 0.0f; + } + + entry = (Entry) iterator.next(); + } + while (!areItemStacksEqual(parItemStack, (ItemStack) entry.getKey())); + + return ((Float) entry.getValue()).floatValue(); + } + +} diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java new file mode 100644 index 0000000..30acf89 --- /dev/null +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java @@ -0,0 +1,99 @@ +package com.quantumindustries.minecraft.mod.blocks.grinder; + +import net.minecraft.entity.item.EntityXPOrb; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.util.math.MathHelper; + +public class SlotGrinderOutput extends Slot { + /** The player that is using the GUI where this slot resides. */ + private final EntityPlayer thePlayer; + private int numGrinderOutput; + + public SlotGrinderOutput(EntityPlayer parPlayer, IInventory parIInventory, + int parSlotIndex, int parXDisplayPosition, int parYDisplayPosition) { + super(parIInventory, parSlotIndex, parXDisplayPosition, parYDisplayPosition); + thePlayer = parPlayer; + } + + /** + * Check if the stack is a valid item for this slot. . + */ + @Override + public boolean isItemValid(ItemStack stack) { + return false; // can't place anything into it + } + + /** + * Decrease the size of the stack in slot by the amount of the int arg. Returns the new + * stack. + */ + @Override + public ItemStack decrStackSize(int parAmount) { + if(getHasStack()) { + numGrinderOutput += Math.min(parAmount, getStack().stackSize); + } + + return super.decrStackSize(parAmount); + } + + @Override + public void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack) { + onCrafting(stack); + super.onPickupFromSlot(playerIn, stack); + } + + /** + * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an + * internal count then calls onCrafting(item). + */ + @Override + protected void onCrafting(ItemStack parItemStack, int parAmountGround) { + numGrinderOutput += parAmountGround; + onCrafting(parItemStack); + } + + /** + * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. + */ + @Override + protected void onCrafting(ItemStack parItemStack) { + if(!thePlayer.worldObj.isRemote) { + int expEarned = numGrinderOutput; + float expFactor = GrinderRecipes.instance().getGrindingExperience(parItemStack); + + if(expFactor == 0.0f) { + expEarned = 0; + } + else if (expFactor < 1.0f) { + int possibleExpEarned = MathHelper.floor_float(expEarned * expFactor); + + if(possibleExpEarned < MathHelper.ceiling_float_int(expEarned*expFactor) && + Math.random() < expEarned*expFactor - possibleExpEarned) { + ++possibleExpEarned; + } + + expEarned = possibleExpEarned; + } + + // create experience orbs + int expInOrb; + while(expEarned > 0) { + expInOrb = EntityXPOrb.getXPSplit(expEarned); + expEarned -= expInOrb; + thePlayer.worldObj.spawnEntityInWorld(new EntityXPOrb( + thePlayer.worldObj, + thePlayer.posX, + thePlayer.posY + 0.5D, + thePlayer.posZ + 0.5D, + expInOrb + )); + } + } + + numGrinderOutput = 0; + } + +} diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java new file mode 100644 index 0000000..e46980e --- /dev/null +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -0,0 +1,451 @@ +package com.quantumindustries.minecraft.mod.blocks.grinder; + +import com.quantumindustries.minecraft.mod.CustomMod; +import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.Container; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.ISidedInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.ITickable; +import net.minecraft.tileentity.TileEntityLockable; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.EnumFacing; +import net.minecraft.world.World; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +public class TileEntityGrinder extends TileEntityLockable + implements ITickable, ISidedInventory { + public enum slotEnum { + INPUT_SLOT, OUTPUT_SLOT + } + private static final int[] slotsTop = new int[] {slotEnum.INPUT_SLOT.ordinal()}; + private static final int[] slotsBottom = new int[] {slotEnum.OUTPUT_SLOT.ordinal()}; + private static final int[] slotsSides = new int[] {}; + /** The ItemStacks that hold the items currently being used in the grinder */ + private ItemStack[] grinderItemStackArray = new ItemStack[2]; + /** The number of ticks that the grinder will keep grinding */ + private int timeCanGrind; + /** The number of ticks that a fresh copy of the currently-grinding item would keep the grinder grinding for */ + private int currentItemGrindTime; + private int ticksGrindingItemSoFar; + private int ticksPerItem; + private String grinderCustomName; + + /** + * This controls whether the tile entity gets replaced whenever the block state is changed. + * Normally only want this when block actually is replaced. + */ + @Override + public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, + IBlockState newSate) { + return (oldState.getBlock() != newSate.getBlock()); + } + + /** + * Returns the number of slots in the inventory. + */ + @Override + public int getSizeInventory() { + return grinderItemStackArray.length; + } + + /** + * Returns the stack in slot i + */ + @Override + public ItemStack getStackInSlot(int index) { + return grinderItemStackArray[index]; + } + + /** + * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a + * new stack. + */ + @Override + public ItemStack decrStackSize(int index, int count) { + if(grinderItemStackArray[index] != null) { + ItemStack itemstack; + + if(grinderItemStackArray[index].stackSize <= count) { + itemstack = grinderItemStackArray[index]; + grinderItemStackArray[index] = null; + return itemstack; + } + else { + itemstack = grinderItemStackArray[index].splitStack(count); + + if(grinderItemStackArray[index].stackSize == 0) { + grinderItemStackArray[index] = null; + } + + return itemstack; + } + } + else { + return null; + } + } + /** + * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem - + * like when you close a workbench GUI. + */ + @Override + public ItemStack removeStackFromSlot(int index) { + if(grinderItemStackArray[index] != null) { + ItemStack itemstack = grinderItemStackArray[index]; + grinderItemStackArray[index] = null; + return itemstack; + } + else { + return null; + } + } + + /** + * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections). + */ + @Override + public void setInventorySlotContents(int index, ItemStack stack) { + + boolean isSameItemStackAlreadyInSlot = stack != null && + stack.isItemEqual(grinderItemStackArray[index]) && + ItemStack.areItemStackTagsEqual(stack, grinderItemStackArray[index]); + grinderItemStackArray[index] = stack; + + if(stack != null && stack.stackSize > getInventoryStackLimit()) { + stack.stackSize = getInventoryStackLimit(); + } + + // if input slot, reset the grinding timers + if(index == slotEnum.INPUT_SLOT.ordinal() && !isSameItemStackAlreadyInSlot) { + ticksPerItem = timeToGrindOneItem(stack); + ticksGrindingItemSoFar = 0; + markDirty(); + } + } + + @Override + public String getName() { + if(hasCustomName()) { + return grinderCustomName; + } + else { + return "container.grinder"; + } + } + + /** + * Returns true if this thing is named + */ + @Override + public boolean hasCustomName() { + return grinderCustomName != null && grinderCustomName.length() > 0; + } + + public void setCustomInventoryName(String parCustomName) { + grinderCustomName = parCustomName; + } + + @Override + public void readFromNBT(NBTTagCompound compound) { + super.readFromNBT(compound); + NBTTagList nbttaglist = compound.getTagList("Items", 10); + grinderItemStackArray = new ItemStack[getSizeInventory()]; + + for(int i = 0; i < nbttaglist.tagCount(); ++i) { + NBTTagCompound nbtTagCompound = nbttaglist.getCompoundTagAt(i); + byte slot = nbtTagCompound.getByte("Slot"); + + if(slot >= 0 && slot < grinderItemStackArray.length) { + grinderItemStackArray[slot] = ItemStack.loadItemStackFromNBT(nbtTagCompound); + } + } + + timeCanGrind = compound.getShort("GrindTime"); + ticksGrindingItemSoFar = compound.getShort("CookTime"); + ticksPerItem = compound.getShort("CookTimeTotal"); + + if(compound.hasKey("CustomName", 8)) { + grinderCustomName = compound.getString("CustomName"); + } + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound compound) { + super.writeToNBT(compound); + compound.setShort("GrindTime", (short) timeCanGrind); + compound.setShort("CookTime", (short) ticksGrindingItemSoFar); + compound.setShort("CookTimeTotal", (short) ticksPerItem); + NBTTagList nbttaglist = new NBTTagList(); + + for(int i = 0; i < grinderItemStackArray.length; ++i) { + if(grinderItemStackArray[i] != null) { + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + nbtTagCompound.setByte("Slot", (byte) i); + grinderItemStackArray[i].writeToNBT(nbtTagCompound); + nbttaglist.appendTag(nbtTagCompound); + } + } + + compound.setTag("Items", nbttaglist); + + if(hasCustomName()) { + compound.setString("CustomName", grinderCustomName); + } + + return compound; + } + + /** + * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't + * this more of a set than a get?* + */ + @Override + public int getInventoryStackLimit() { + return 64; + } + + /** + * Grinder is grinding + */ + public boolean grindingSomething() { + return true; + } + + // this function indicates whether container texture should be drawn + @SideOnly(Side.CLIENT) + public static boolean func_174903_a(IInventory parIInventory) { + return true; + } + + @Override + public void update() { + boolean hasBeenGrinding = grindingSomething(); + boolean changedGrindingState = false; + + if(grindingSomething()) { + --timeCanGrind; + } + + if(!worldObj.isRemote) { + // if something in input slot + if(grinderItemStackArray[slotEnum.INPUT_SLOT.ordinal()] != null) { + // start grinding + if(!grindingSomething() && canGrind()) { + timeCanGrind = 150; + + if(grindingSomething()) { + changedGrindingState = true; + } + } + + // continue grinding + if(grindingSomething() && canGrind()) { + ++ticksGrindingItemSoFar; + + // check if completed grinding an item + if(ticksGrindingItemSoFar == ticksPerItem) { + ticksGrindingItemSoFar = 0; + ticksPerItem = timeToGrindOneItem(grinderItemStackArray[0]); + grindItem(); + changedGrindingState = true; + } + } + else { + ticksGrindingItemSoFar = 0; + } + } + + // started or stopped grinding, update block to change to active or inactive model + if(hasBeenGrinding != grindingSomething()) { + // the isGrinding() value may have changed due to call to grindItem() earlier + changedGrindingState = true; + } + } + + if(changedGrindingState) { + markDirty(); + } + } + + public int timeToGrindOneItem(ItemStack parItemStack) { + return 200; + } + + /** + * Returns true if the grinder can grind an item, i.e. has a source item, destination stack isn't full, etc. + */ + private boolean canGrind() { + int inputSlot = slotEnum.INPUT_SLOT.ordinal(); + int outputSlot = slotEnum.OUTPUT_SLOT.ordinal(); + ItemStack inputStack = grinderItemStackArray[inputSlot]; + ItemStack outputStack = grinderItemStackArray[outputSlot]; + // if nothing in input slot + if(inputStack == null) { + return false; + } + else { // check if it has a grinding recipe + ItemStack itemStackToOutput = GrinderRecipes.instance().getGrindingResult(inputStack); + if(itemStackToOutput == null) { + return false; // no valid recipe for grinding this item + } + if(outputStack == null) { + return true; // output slot is empty + } + if(!outputStack.isItemEqual(itemStackToOutput)) { + return false; // output slot has different item occupying it + } + // check if output slot is full + int result = outputStack.stackSize + itemStackToOutput.stackSize; + return result <= getInventoryStackLimit() && + result <= outputStack.getMaxStackSize(); + } + } + + /** + * Turn one item from the grinder source stack into the appropriate grinded item in the grinder result stack + */ + public void grindItem() { + if(canGrind()) { + int inputSlot = slotEnum.INPUT_SLOT.ordinal(); + int outputSlot = slotEnum.OUTPUT_SLOT.ordinal(); + ItemStack inputStack = grinderItemStackArray[inputSlot]; + ItemStack outputStack = grinderItemStackArray[outputSlot]; + + ItemStack itemstack = GrinderRecipes.instance().getGrindingResult(inputStack); + + // check if output slot is empty + if(outputStack == null) { + grinderItemStackArray[outputSlot] = itemstack.copy(); + } + else if(outputStack.getItem() == itemstack.getItem()) { + outputStack.stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items + } + + --inputStack.stackSize; + + if(inputStack.stackSize <= 0) { + grinderItemStackArray[inputSlot] = null; + } + } + } + + /** + * Do not make give this method the name canInteractWith because it clashes with Container + */ + @Override + public boolean isUseableByPlayer(EntityPlayer playerIn) { + if(worldObj.getTileEntity(pos) != this) { + return false; + } + else { + return playerIn.getDistanceSq( + pos.getX() + 0.5D, + pos.getY() + 0.5D, + pos.getZ() + 0.5D + ) <= 64.0D; + } + } + + @Override + public void openInventory(EntityPlayer playerIn) {} + + @Override + public void closeInventory(EntityPlayer playerIn) {} + + @Override + public boolean isItemValidForSlot(int index, ItemStack stack) { + // can always put things in input (may not grind though) and can't put anything in output + return index == slotEnum.INPUT_SLOT.ordinal(); + } + + @Override + public int[] getSlotsForFace(EnumFacing side) { + if(side == EnumFacing.DOWN) { + return slotsBottom; + } + else { + if(side == EnumFacing.UP) { + return slotsTop; + } + else { + return slotsSides; + } + } + } + + /** + * Returns true if automation can insert the given item in the given slot from the given side. Args: slot, item, + * side + */ + @Override + public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) { + return isItemValidForSlot(index, itemStackIn); + } + + /** + * Returns true if automation can extract the given item in the given slot from the given side. Args: slot, item, + * side + */ + @Override + public boolean canExtractItem(int parSlotIndex, ItemStack parStack, EnumFacing parFacing) { + return true; + } + + @Override + public String getGuiID() { + return CustomMod.MODID + ":grinder"; + } + + @Override + public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) { + return new ContainerGrinder(playerInventory, this); + } + + @Override + public int getField(int id) { + switch(id) { + case 0: + return timeCanGrind; + case 1: + return currentItemGrindTime; + case 2: + return ticksGrindingItemSoFar; + case 3: + return ticksPerItem; + } + return 0; + } + + @Override + public void setField(int id, int value) { + switch(id) { + case 0: + timeCanGrind = value; + case 1: + currentItemGrindTime = value; + case 2: + ticksGrindingItemSoFar = value; + case 3: + ticksPerItem = value; + } + } + + @Override + public int getFieldCount() { + return 4; + } + + @Override + public void clear() { + for(int i = 0; i < grinderItemStackArray.length; ++i) { + grinderItemStackArray[i] = null; + } + } + +} diff --git a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java new file mode 100644 index 0000000..a580374 --- /dev/null +++ b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java @@ -0,0 +1,80 @@ +package com.quantumindustries.minecraft.mod.guis; + +import com.quantumindustries.minecraft.mod.CustomMod; +import com.quantumindustries.minecraft.mod.blocks.grinder.ContainerGrinder; +import net.minecraft.client.gui.inventory.GuiContainer; +import net.minecraft.client.renderer.GlStateManager; +import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +@SideOnly(Side.CLIENT) +public class GuiGrinder extends GuiContainer { + + private static final ResourceLocation grinderGuiTextures = new ResourceLocation( + CustomMod.MODID + ":textures/gui/container/grinder.png" + ); + private final InventoryPlayer inventoryPlayer; + private final IInventory tileGrinder; + + public GuiGrinder(InventoryPlayer parInventoryPlayer, IInventory parInventoryGrinder) { + super(new ContainerGrinder(parInventoryPlayer, parInventoryGrinder)); + inventoryPlayer = parInventoryPlayer; + tileGrinder = parInventoryGrinder; + } + + /** + * Draw the foreground layer for the GuiContainer (everything in front of the items). Args : mouseX, mouseY + */ + @Override + protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { + String s = tileGrinder.getDisplayName().getUnformattedText(); + fontRendererObj.drawString( + s, + xSize/2 - fontRendererObj.getStringWidth(s)/2, + 6, + 4210752 + ); + fontRendererObj.drawString( + inventoryPlayer.getDisplayName().getUnformattedText(), + 8, + ySize - 96 + 2, + 4210752 + ); + } + + /** + * Args : renderPartialTicks, mouseX, mouseY + */ + @Override + protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { + GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f); + mc.getTextureManager().bindTexture(grinderGuiTextures); + int marginHorizontal = (width - xSize)/2; + int marginVertical = (height - ySize)/2; + drawTexturedModalRect( + marginHorizontal, marginVertical, + 0, 0, xSize, ySize + ); + + int progressLevel = getProgressLevel(24); + drawTexturedModalRect( + marginHorizontal + 79, marginVertical + 34, + 176, 14, + progressLevel + 1, 16 + ); + } + + private int getProgressLevel(int progressIndicatorPixelWidth) { + int ticksGrindingItemSoFar = tileGrinder.getField(2); + int ticksPerItem = tileGrinder.getField(3); + + if(ticksPerItem != 0 && ticksGrindingItemSoFar != 0) { + return ticksGrindingItemSoFar*progressIndicatorPixelWidth/ticksPerItem; + } + return 0; + } + +} diff --git a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java new file mode 100644 index 0000000..24f99d2 --- /dev/null +++ b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java @@ -0,0 +1,39 @@ +package com.quantumindustries.minecraft.mod.guis; + +import com.quantumindustries.minecraft.mod.CustomMod; +import com.quantumindustries.minecraft.mod.blocks.grinder.ContainerGrinder; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.network.IGuiHandler; + +public class GuiHandler implements IGuiHandler { + + @Override + public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { + TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); + + if(tileEntity != null) { + if(ID == CustomMod.GUI_ENUM.GRINDER.ordinal()) { + return new ContainerGrinder(player.inventory, (IInventory)tileEntity); + } + } + + return null; + } + + @Override + public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { + TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); + + if(tileEntity != null) { + if(ID == CustomMod.GUI_ENUM.GRINDER.ordinal()) { + return new GuiGrinder(player.inventory, (IInventory)tileEntity); + } + } + return null; + } + +} diff --git a/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java b/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java index b915783..0bdc10c 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java @@ -1,12 +1,14 @@ package com.quantumindustries.minecraft.mod.proxy; import com.quantumindustries.minecraft.mod.CustomMod; +import com.quantumindustries.minecraft.mod.guis.GuiHandler; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.item.Item; import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.network.NetworkRegistry; public class CommonProxy { @@ -16,6 +18,10 @@ public void preInit(FMLPreInitializationEvent event) { public void init(FMLInitializationEvent event) { // TODO + NetworkRegistry.INSTANCE.registerGuiHandler( + CustomMod.instance, + new GuiHandler() + ); } public void postInit(FMLPostInitializationEvent event) { diff --git a/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java b/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java new file mode 100644 index 0000000..fac48c7 --- /dev/null +++ b/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java @@ -0,0 +1,40 @@ +package com.quantumindustries.minecraft.mod.tileentities; + +import com.quantumindustries.minecraft.mod.CustomMod; +import com.quantumindustries.minecraft.mod.ItemModelProvider; +import com.quantumindustries.minecraft.mod.blocks.BlockBase; +import net.minecraft.block.BlockContainer; +import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; + +import javax.annotation.Nullable; +public abstract class BlockContainerTileEntity + extends BlockContainer implements ItemModelProvider { + + public BlockContainerTileEntity(Material material, String name) { + super(material); + setUnlocalizedName(name); + setRegistryName(name); + setCreativeTab(CustomMod.tab); + } + + public abstract Class getTileEntityClass(); + + public TE getTileEntity(IBlockAccess world, BlockPos pos) { + return (TE) world.getTileEntity(pos); + } + + @Override + public boolean hasTileEntity(IBlockState state) { + return true; + } + + @Nullable + @Override + public abstract TE createTileEntity(World world, IBlockState state); + +} diff --git a/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockTileEntity.java b/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockTileEntity.java index 9846fc2..ab29838 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockTileEntity.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockTileEntity.java @@ -18,7 +18,7 @@ public BlockTileEntity(Material material, String name) { public abstract Class getTileEntityClass(); public TE getTileEntity(IBlockAccess world, BlockPos pos) { - return (TE)world.getTileEntity(pos); + return (TE) world.getTileEntity(pos); } @Override diff --git a/src/main/resources/assets/custommod/blockstates/grinder.json b/src/main/resources/assets/custommod/blockstates/grinder.json new file mode 100644 index 0000000..6157aeb --- /dev/null +++ b/src/main/resources/assets/custommod/blockstates/grinder.json @@ -0,0 +1,9 @@ +{ + "forge_marker": 1, + "variants": { + "facing=north": { "model": "blocksmith:grinder" }, + "facing=south": { "model": "blocksmith:grinder", "y": 180 }, + "facing=west": { "model": "blocksmith:grinder", "y": 270 }, + "facing=east": { "model": "blocksmith:grinder", "y": 90 } + } +} diff --git a/src/main/resources/assets/custommod/lang/en_US.lang b/src/main/resources/assets/custommod/lang/en_US.lang index 1fedfad..1a60f4f 100644 --- a/src/main/resources/assets/custommod/lang/en_US.lang +++ b/src/main/resources/assets/custommod/lang/en_US.lang @@ -19,3 +19,5 @@ tile.blockRhodium.name=Rhodium Block tile.blockFluidNitrogen.name=Liquid Nitrogen tile.blockFluidOxygen.name=Liquid Oxygen tile.blockFluidArgon.name=Liquid Argon + +container.grinder=Grinder diff --git a/src/main/resources/assets/custommod/models/block/grinder.json b/src/main/resources/assets/custommod/models/block/grinder.json new file mode 100644 index 0000000..275d616 --- /dev/null +++ b/src/main/resources/assets/custommod/models/block/grinder.json @@ -0,0 +1,9 @@ +{ + "parent": "block/orientable", + "textures": + { + "top": "blocks/furnace_top", + "front": "blocks/furnace_front_off", + "side": "blocks/furnace_side" + } +} diff --git a/src/main/resources/assets/custommod/models/item/grinder.json b/src/main/resources/assets/custommod/models/item/grinder.json new file mode 100644 index 0000000..599a8c8 --- /dev/null +++ b/src/main/resources/assets/custommod/models/item/grinder.json @@ -0,0 +1,10 @@ +{ + "parent": "blocksmith:block/grinder", + "display": { + "thirdperson": { + "rotation": [ 10, -45, 170 ], + "translation": [ 0, 1.5, -2.75 ], + "scale": [ 0.375, 0.375, 0.375 ] + } + } +} diff --git a/src/main/resources/assets/custommod/textures/gui/container/grinder.png b/src/main/resources/assets/custommod/textures/gui/container/grinder.png new file mode 100644 index 0000000000000000000000000000000000000000..6a4f7741a4a1d7d7f282b5152bc817a3ae0334ee GIT binary patch literal 1416 zcmcIjYcQNw7(U;(E^Cdo>r$5`-9e)vL=qDw2+a_-E{#xYn}}`Q>$*uz7R~xp+$tfe zH5!JEXfa!jT6HIjiDf7o3F8u^L^jl$`_Yx_z2qiOoDlXK?G^PcCN_kGX%oOANd z`gv**j0pe$Et;46IRGHcgaBR*gA12xBQa3>%-ho)DEB+JtEL!ZG?Kg+mjNL5><_55 zD0Uem)xV(mdZ^Fi^mNQ^0{9DF0I2HI+&>6pPcP59`kB*B1Z$P%M5Gnem6%yJgv9Av z5ZZ75kTWt?P2+#a|IMr|uf#m=(<^$`SyoxXD5`#&+qV=SLReTonrGPQbX>YobQ5O3 zJ0{qbcO6aHT)1c?B6cmM>v{OETt!JrR6dihj8?ERd${6bXV8XnG=h=skQmSAonn}l zhYII}+LYoh`CgAKc{uVozgq`cU#U86t>>{ug*Rr+h$DCJ7-va#H)eb9^Cs&!%u|&e zR`%0u=Yrzm;!>Z3OOmO*&s`%8_=d!se09@h;oREv#FG`p@5)K5>Q+@dSxmizuULqZ z4dwG-D+mcXIbE1L<0*w6OJ#}X1utiDB+wq7%$L6CE0e3!(_OxNGAk}rqJ18^p#4|$ z-i@+RS9(=aTFbHEV9uO11iOh#lw`V8o9+M%{CAEf6wKEjXl^yyu_NF?eaqyo*j3TE zw#M7e;cd&6#0ypsH#VU_n{J&kKE_#Qa1;?e_ZR2eqG31sVR~`gG z+@&Dz-7PxQz1MYXllO7Ytk|K25HrKlh~~=@$o%~L+?~wVjO0Kiywtok<$DzJs{;3hX}6oID5Uy^iAn8L~4_Fb5||lGL*# z1&hy`US0hg#s4h_9`VNXu^8C*Mjhh{12SSvYv399m1u)D$`pzd`N4~=)Chk)TU(2E z__>xEj-EM@gjbN~4?^Q%7r--sA; literal 0 HcmV?d00001 From e431f050fe15c19314773ddf710933f6a92969a7 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 12 Mar 2017 21:52:55 -0500 Subject: [PATCH 03/15] Grinder is operational --- .gitignore | 1 + run/eula.txt | 3 +++ .../minecraft/mod/CustomMod.java | 2 +- .../minecraft/mod/blocks/ModBlocks.java | 14 ++++++++---- .../mod/blocks/grinder/BlockGrinder.java | 20 ----------------- .../mod/blocks/grinder/ContainerGrinder.java | 2 ++ .../mod/blocks/grinder/TileEntityGrinder.java | 10 +++++---- .../BlockInfiniteProducer.java | 1 + .../minecraft/mod/guis/GuiGrinder.java | 2 +- .../minecraft/mod/guis/GuiHandler.java | 12 ++++++++-- .../minecraft/mod/proxy/ClientProxy.java | 22 ++++++++++++++++++- .../minecraft/mod/proxy/CommonProxy.java | 4 +++- .../BlockContainerTileEntity.java | 3 ++- .../custommod/blockstates/blockGrinder.json | 9 ++++++++ .../assets/custommod/blockstates/grinder.json | 9 -------- .../block/{grinder.json => blockGrinder.json} | 0 .../item/{grinder.json => blockGrinder.json} | 2 +- 17 files changed, 71 insertions(+), 45 deletions(-) create mode 100644 run/eula.txt create mode 100644 src/main/resources/assets/custommod/blockstates/blockGrinder.json delete mode 100644 src/main/resources/assets/custommod/blockstates/grinder.json rename src/main/resources/assets/custommod/models/block/{grinder.json => blockGrinder.json} (100%) rename src/main/resources/assets/custommod/models/item/{grinder.json => blockGrinder.json} (78%) diff --git a/.gitignore b/.gitignore index efa356a..d7f5e11 100644 --- a/.gitignore +++ b/.gitignore @@ -70,3 +70,4 @@ gradle/ gradlew gradlew.bat logs/ +!/run/eula.txt diff --git a/run/eula.txt b/run/eula.txt new file mode 100644 index 0000000..f943552 --- /dev/null +++ b/run/eula.txt @@ -0,0 +1,3 @@ +#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula). +#Sun Mar 12 21:15:07 CDT 2017 +eula=true diff --git a/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java b/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java index 1b2cd7b..0fb00bb 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java @@ -19,7 +19,7 @@ public class CustomMod { serverSide = "com.quantumindustries.minecraft.mod.proxy.CommonProxy") public static CommonProxy proxy; - @Mod.Instance + @Mod.Instance(MODID) public static CustomMod instance; public static final CustomTab tab = new CustomTab(); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java index ec52c18..5086204 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java @@ -29,8 +29,8 @@ public class ModBlocks { public static void init() { initOres(); initOreBlocks(); - register(new BlockInfiniteProducer()); - register(new BlockPowerAnalyzer()); + /*register(new BlockInfiniteProducer()); + register(new BlockPowerAnalyzer());*/ initBlockGrinder(); } @@ -61,14 +61,20 @@ private static T register(T block, ItemBlock itemBlock) { ((ItemModelProvider) block).registerItemModel(itemBlock); } - if(block instanceof BlockTileEntity || - block instanceof BlockContainerTileEntity) { + if(block instanceof BlockTileEntity) { GameRegistry.registerTileEntity( ((BlockTileEntity) block).getTileEntityClass(), block.getRegistryName().toString() ); } + if(block instanceof BlockContainerTileEntity) { + GameRegistry.registerTileEntity( + ((BlockContainerTileEntity) block).getTileEntityClass(), + block.getRegistryName().toString() + ); + } + if(itemBlock instanceof ItemOreDict) { ((ItemOreDict) itemBlock).initOreDict(); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java index d22714c..48a1e85 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java @@ -11,9 +11,6 @@ import net.minecraft.block.properties.PropertyDirection; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.RenderItem; -import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.InventoryHelper; @@ -169,10 +166,6 @@ public int getMetaFromState(IBlockState state) { return state.getValue(FACING).getIndex(); } - public boolean isGrinding() { - return isGrinding; - } - @Override public Class getTileEntityClass() { return TileEntityGrinder.class; @@ -184,19 +177,6 @@ public TileEntityGrinder createTileEntity(World world, IBlockState state) { return new TileEntityGrinder(); } - @Override - public void registerItemModel(Item item) { - RenderItem renderItem = Minecraft.getMinecraft().getRenderItem(); - renderItem.getItemModelMesher().register( - Item.getItemFromBlock(this), - 0, - new ModelResourceLocation( - CustomMod.MODID + ":" + getUnlocalizedName(), - "inventory" - ) - ); - } - @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] { FACING }); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index a77ed92..5fb9925 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -19,6 +19,8 @@ public class ContainerGrinder extends Container { private int timeCanGrind; public ContainerGrinder(InventoryPlayer parInventoryPlayer, IInventory parIInventory) { + System.out.println("DEBUG: ContainerGrinder constructor()"); + tileGrinder = parIInventory; sizeInventory = tileGrinder.getSizeInventory(); addSlotToContainer( diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index e46980e..c46b65f 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -20,11 +20,12 @@ public class TileEntityGrinder extends TileEntityLockable implements ITickable, ISidedInventory { + public enum slotEnum { INPUT_SLOT, OUTPUT_SLOT } - private static final int[] slotsTop = new int[] {slotEnum.INPUT_SLOT.ordinal()}; - private static final int[] slotsBottom = new int[] {slotEnum.OUTPUT_SLOT.ordinal()}; + private static final int[] slotsTop = new int[] { slotEnum.INPUT_SLOT.ordinal() }; + private static final int[] slotsBottom = new int[] { slotEnum.OUTPUT_SLOT.ordinal() }; private static final int[] slotsSides = new int[] {}; /** The ItemStacks that hold the items currently being used in the grinder */ private ItemStack[] grinderItemStackArray = new ItemStack[2]; @@ -43,7 +44,7 @@ public enum slotEnum { @Override public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) { - return (oldState.getBlock() != newSate.getBlock()); + return oldState.getBlock() != newSate.getBlock(); } /** @@ -399,11 +400,12 @@ public boolean canExtractItem(int parSlotIndex, ItemStack parStack, EnumFacing p @Override public String getGuiID() { - return CustomMod.MODID + ":grinder"; + return CustomMod.MODID + ":blockGrinder"; } @Override public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) { + System.out.println("DEBUG: TileEntityGrinder createContainer()"); return new ContainerGrinder(playerInventory, this); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/infiniteproducer/BlockInfiniteProducer.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/infiniteproducer/BlockInfiniteProducer.java index 8fd96ad..83224aa 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/infiniteproducer/BlockInfiniteProducer.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/infiniteproducer/BlockInfiniteProducer.java @@ -21,6 +21,7 @@ public Class getTileEntityClass() { @Nullable @Override public TileEntityInfiniteProducer createTileEntity(World world, IBlockState state) { + System.out.println("DEBUG: createTileEntity() TileEntityInfiniteProducer"); return new TileEntityInfiniteProducer(); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java index a580374..14d7b21 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java @@ -11,7 +11,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) -public class GuiGrinder extends GuiContainer { +public class GuiGrinder extends GuiContainer { private static final ResourceLocation grinderGuiTextures = new ResourceLocation( CustomMod.MODID + ":textures/gui/container/grinder.png" diff --git a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java index 24f99d2..a1bae47 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java @@ -11,13 +11,19 @@ public class GuiHandler implements IGuiHandler { + public GuiHandler() { + System.out.println("DEBUG: GuiHandler created"); + } + @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); + System.out.println("DEBUG: getServerGuiElement() called"); + if(tileEntity != null) { if(ID == CustomMod.GUI_ENUM.GRINDER.ordinal()) { - return new ContainerGrinder(player.inventory, (IInventory)tileEntity); + return new ContainerGrinder(player.inventory, (IInventory) tileEntity); } } @@ -28,9 +34,11 @@ public Object getServerGuiElement(int ID, EntityPlayer player, World world, int public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); + System.out.println("DEBUG: getClientGuiElement() called"); + if(tileEntity != null) { if(ID == CustomMod.GUI_ENUM.GRINDER.ordinal()) { - return new GuiGrinder(player.inventory, (IInventory)tileEntity); + return new GuiGrinder(player.inventory, (IInventory) tileEntity); } } return null; diff --git a/src/main/java/com/quantumindustries/minecraft/mod/proxy/ClientProxy.java b/src/main/java/com/quantumindustries/minecraft/mod/proxy/ClientProxy.java index 3809f74..e511289 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/proxy/ClientProxy.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/proxy/ClientProxy.java @@ -1,10 +1,15 @@ package com.quantumindustries.minecraft.mod.proxy; +import com.quantumindustries.minecraft.mod.CustomMod; import com.quantumindustries.minecraft.mod.ModWorldGen; import com.quantumindustries.minecraft.mod.blocks.ModBlocks; import com.quantumindustries.minecraft.mod.fluids.ModFluids; import com.quantumindustries.minecraft.mod.items.ModItems; import com.quantumindustries.minecraft.mod.recipes.ModRecipes; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.RenderItem; +import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.item.Item; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; @@ -19,16 +24,31 @@ public void preInit(FMLPreInitializationEvent event) { ModItems.init(); ModRecipes.init(); GameRegistry.registerWorldGenerator(new ModWorldGen(), 3); + + System.out.println("DEBUG: Client proxy preInit()"); } @Override public void init(FMLInitializationEvent event) { - // TODO + RenderItem renderItem = Minecraft.getMinecraft().getRenderItem(); + renderItem.getItemModelMesher().register( + Item.getItemFromBlock(ModBlocks.blockGrinder), + 0, + new ModelResourceLocation( + CustomMod.MODID + ":" + ModBlocks.blockGrinder + .getUnlocalizedName(), + "inventory" + ) + ); + super.init(event); + + System.out.println("DEBUG: Client proxy init()"); } @Override public void postInit(FMLPostInitializationEvent event) { // TODO + System.out.println("DEBUG: Client proxy postInit()"); } } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java b/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java index 0bdc10c..c10940d 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java @@ -14,18 +14,20 @@ public class CommonProxy { public void preInit(FMLPreInitializationEvent event) { // TODO + System.out.println("DEBUG: Server proxy preInit()"); } public void init(FMLInitializationEvent event) { - // TODO NetworkRegistry.INSTANCE.registerGuiHandler( CustomMod.instance, new GuiHandler() ); + System.out.println("DEBUG: Server proxy init()"); } public void postInit(FMLPostInitializationEvent event) { // TODO + System.out.println("DEBUG: Server proxy postInit()"); } public void registerItemRenderer(Item item, int meta, String id) { diff --git a/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java b/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java index fac48c7..e0b3857 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java @@ -13,7 +13,7 @@ import javax.annotation.Nullable; public abstract class BlockContainerTileEntity - extends BlockContainer implements ItemModelProvider { + extends BlockContainer { public BlockContainerTileEntity(Material material, String name) { super(material); @@ -30,6 +30,7 @@ public TE getTileEntity(IBlockAccess world, BlockPos pos) { @Override public boolean hasTileEntity(IBlockState state) { + System.out.println("DEBUG: hasTileEntity"); return true; } diff --git a/src/main/resources/assets/custommod/blockstates/blockGrinder.json b/src/main/resources/assets/custommod/blockstates/blockGrinder.json new file mode 100644 index 0000000..5b870be --- /dev/null +++ b/src/main/resources/assets/custommod/blockstates/blockGrinder.json @@ -0,0 +1,9 @@ +{ + "forge_marker": 1, + "variants": { + "facing=north": { "model": "custommod:blockGrinder" }, + "facing=south": { "model": "custommod:blockGrinder", "y": 180 }, + "facing=west": { "model": "custommod:blockGrinder", "y": 270 }, + "facing=east": { "model": "custommod:blockGrinder", "y": 90 } + } +} diff --git a/src/main/resources/assets/custommod/blockstates/grinder.json b/src/main/resources/assets/custommod/blockstates/grinder.json deleted file mode 100644 index 6157aeb..0000000 --- a/src/main/resources/assets/custommod/blockstates/grinder.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "forge_marker": 1, - "variants": { - "facing=north": { "model": "blocksmith:grinder" }, - "facing=south": { "model": "blocksmith:grinder", "y": 180 }, - "facing=west": { "model": "blocksmith:grinder", "y": 270 }, - "facing=east": { "model": "blocksmith:grinder", "y": 90 } - } -} diff --git a/src/main/resources/assets/custommod/models/block/grinder.json b/src/main/resources/assets/custommod/models/block/blockGrinder.json similarity index 100% rename from src/main/resources/assets/custommod/models/block/grinder.json rename to src/main/resources/assets/custommod/models/block/blockGrinder.json diff --git a/src/main/resources/assets/custommod/models/item/grinder.json b/src/main/resources/assets/custommod/models/item/blockGrinder.json similarity index 78% rename from src/main/resources/assets/custommod/models/item/grinder.json rename to src/main/resources/assets/custommod/models/item/blockGrinder.json index 599a8c8..1914fb6 100644 --- a/src/main/resources/assets/custommod/models/item/grinder.json +++ b/src/main/resources/assets/custommod/models/item/blockGrinder.json @@ -1,5 +1,5 @@ { - "parent": "blocksmith:block/grinder", + "parent": "custommod:block/blockGrinder", "display": { "thirdperson": { "rotation": [ 10, -45, 170 ], From 31d8119db0d78cb719930c17b760bbedf83dd7cb Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 12 Mar 2017 23:28:56 -0500 Subject: [PATCH 04/15] Refactoring and code cleanup --- .../minecraft/mod/CustomMod.java | 2 +- .../minecraft/mod/blocks/ModBlocks.java | 14 +- .../mod/blocks/grinder/BlockGrinder.java | 29 +--- .../mod/blocks/grinder/ContainerGrinder.java | 62 +++---- .../mod/blocks/grinder/GrinderRecipes.java | 60 +++---- .../mod/blocks/grinder/SlotGrinderOutput.java | 22 +-- .../mod/blocks/grinder/TileEntityGrinder.java | 156 +++++++----------- .../minecraft/mod/guis/GuiGrinder.java | 11 +- .../minecraft/mod/guis/GuiHandler.java | 17 +- .../minecraft/mod/proxy/CommonProxy.java | 23 ++- .../BlockContainerTileEntity.java | 6 +- 11 files changed, 155 insertions(+), 247 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java b/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java index 0fb00bb..75ee36b 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/CustomMod.java @@ -24,7 +24,7 @@ public class CustomMod { public static final CustomTab tab = new CustomTab(); - public enum GUI_ENUM { + public enum GUI { GRINDER } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java index 5086204..a2e7237 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java @@ -1,6 +1,5 @@ package com.quantumindustries.minecraft.mod.blocks; -import com.quantumindustries.minecraft.mod.CustomMod; import com.quantumindustries.minecraft.mod.ItemModelProvider; import com.quantumindustries.minecraft.mod.blocks.grinder.BlockGrinder; import com.quantumindustries.minecraft.mod.blocks.infiniteproducer.BlockInfiniteProducer; @@ -9,10 +8,6 @@ import com.quantumindustries.minecraft.mod.tileentities.BlockContainerTileEntity; import com.quantumindustries.minecraft.mod.tileentities.BlockTileEntity; import net.minecraft.block.Block; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.RenderItem; -import net.minecraft.client.renderer.block.model.ModelResourceLocation; -import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraftforge.fml.common.registry.GameRegistry; @@ -29,8 +24,9 @@ public class ModBlocks { public static void init() { initOres(); initOreBlocks(); - /*register(new BlockInfiniteProducer()); - register(new BlockPowerAnalyzer());*/ + + register(new BlockInfiniteProducer()); + register(new BlockPowerAnalyzer()); initBlockGrinder(); } @@ -49,8 +45,7 @@ private static void initBlockGrinder() { blockGrinder = register(new BlockGrinder()); } - // Registers blocks and checks what they are instanceof - // for further registrations. + // TODO(TT): Refactor to prevent/reduce instanceof checks private static T register(T block, ItemBlock itemBlock) { GameRegistry.register(block); @@ -78,7 +73,6 @@ private static T register(T block, ItemBlock itemBlock) { if(itemBlock instanceof ItemOreDict) { ((ItemOreDict) itemBlock).initOreDict(); } - } return block; diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java index 48a1e85..dc23708 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java @@ -82,7 +82,7 @@ public boolean onBlockActivated(World parWorld, BlockPos parBlockPos, if(!parWorld.isRemote) { parPlayer.openGui( CustomMod.instance, - CustomMod.GUI_ENUM.GRINDER.ordinal(), + CustomMod.GUI.GRINDER.ordinal(), parWorld, parBlockPos.getX(), parBlockPos.getY(), @@ -230,31 +230,4 @@ private boolean shouldFaceWest(IBlockState blockToWest, IBlockState blockToEast, !blockToWest.isFullBlock(); } - @SideOnly(Side.CLIENT) - static final class SwitchEnumFacing { - static final int[] enumFacingArray = new int[EnumFacing.values().length]; - - static { - try { - enumFacingArray[EnumFacing.WEST.ordinal()] = 1; - } - catch(NoSuchFieldError e) {} - - try { - enumFacingArray[EnumFacing.EAST.ordinal()] = 2; - } - catch(NoSuchFieldError e) {} - - try { - enumFacingArray[EnumFacing.NORTH.ordinal()] = 3; - } - catch(NoSuchFieldError e) {} - - try { - enumFacingArray[EnumFacing.SOUTH.ordinal()] = 4; - } - catch(NoSuchFieldError e) {} - } - } - } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index 5fb9925..7894b27 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -19,53 +19,44 @@ public class ContainerGrinder extends Container { private int timeCanGrind; public ContainerGrinder(InventoryPlayer parInventoryPlayer, IInventory parIInventory) { - System.out.println("DEBUG: ContainerGrinder constructor()"); - + // TODO(TT): Break this method up. Too many lines. tileGrinder = parIInventory; sizeInventory = tileGrinder.getSizeInventory(); - addSlotToContainer( - new Slot( - tileGrinder, - TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal(), - 56, - 35 - ) - ); - addSlotToContainer( - new SlotGrinderOutput( - parInventoryPlayer.player, - tileGrinder, - TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal(), - 116, - 35 - ) - ); + addSlotToContainer(new Slot( + tileGrinder, + TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal(), + 56, + 35 + )); + addSlotToContainer(new SlotGrinderOutput( + parInventoryPlayer.player, + tileGrinder, + TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal(), + 116, + 35 + )); // add player inventory slots // note that the slot numbers are within the player inventory so can be same as the tile entity inventory for(int i = 0; i < 3; ++i) { for(int j = 0; j < 9; ++j) { - addSlotToContainer( - new Slot( - parInventoryPlayer, - j + i * 9 + 9, - 8 + j * 18, - 84 + i * 18 - ) - ); + addSlotToContainer(new Slot( + parInventoryPlayer, + j + i * 9 + 9, + 8 + j * 18, + 84 + i * 18 + )); } } // add hotbar slots for(int i = 0; i < 9; ++i) { - addSlotToContainer( - new Slot( - parInventoryPlayer, - i, - 8 + i * 18, - 142 - ) - ); + addSlotToContainer(new Slot( + parInventoryPlayer, + i, + 8 + i * 18, + 142 + )); } } @@ -113,6 +104,7 @@ public boolean canInteractWith(EntityPlayer playerIn) { */ @Override public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) { + // TODO(TT): Break this method up. Too many lines. ItemStack itemStack1 = null; Slot slot = inventorySlots.get(slotIndex); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java index 718f8e4..93a4548 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java @@ -6,6 +6,7 @@ import com.google.common.collect.Maps; +import net.minecraft.block.Block; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.Item; @@ -14,7 +15,6 @@ public class GrinderRecipes { private static final GrinderRecipes grindingBase = new GrinderRecipes(); - /** The list of grinding results. */ private final Map grindingList = Maps.newHashMap(); private final Map experienceList = Maps.newHashMap(); @@ -23,27 +23,34 @@ public static GrinderRecipes instance() { } private GrinderRecipes() { - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.STONEBRICK), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.STONE_SLAB), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.STONE_SLAB2), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SANDSTONE_STAIRS), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.STONE), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.SAND)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SANDSTONE), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.SAND)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.GLASS), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.SAND)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.BRICK_BLOCK), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.GRAVEL)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.PLANKS), 1, 32767), new ItemStack(Items.PAPER, 10), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.LOG), 1, 32767), new ItemStack(Items.PAPER), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.LOG2), 1, 32767), new ItemStack(Items.PAPER), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.NETHER_BRICK), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.NETHERRACK)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.NETHER_BRICK_STAIRS), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.NETHERRACK)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.NETHER_BRICK_FENCE), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.NETHERRACK)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.NETHERRACK), 1, 32767), new ItemStack(Item.getItemFromBlock(Blocks.SOUL_SAND)), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SOUL_SAND), 1, 32767), new ItemStack(Items.GUNPOWDER, 4), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SLIME_BLOCK), 1, 32767), new ItemStack(Items.SLIME_BALL, 9), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.OBSIDIAN), 1, 32767), new ItemStack(Items.FLINT, 10), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.PRISMARINE), 1, 32767), new ItemStack(Items.PRISMARINE_SHARD, 10), 0.7f); - addGrindingRecipe(new ItemStack(Item.getItemFromBlock(Blocks.SEA_LANTERN), 1, 32767), new ItemStack(Items.PRISMARINE_CRYSTALS, 9), 0.7f); + initRecipe(Blocks.STONEBRICK, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.STONE_SLAB, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.STONE_SLAB2, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.SANDSTONE_STAIRS, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.STONE, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.GRAVEL, Item.getItemFromBlock(Blocks.SAND), 1); + initRecipe(Blocks.SANDSTONE, Item.getItemFromBlock(Blocks.SAND), 1); + initRecipe(Blocks.GLASS, Item.getItemFromBlock(Blocks.SAND), 1); + initRecipe(Blocks.BRICK_BLOCK, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.PLANKS, Items.PAPER, 10); + initRecipe(Blocks.LOG, Items.PAPER, 1); + initRecipe(Blocks.LOG2, Items.PAPER, 1); + initRecipe(Blocks.NETHER_BRICK, Item.getItemFromBlock(Blocks.NETHERRACK), 1); + initRecipe(Blocks.NETHER_BRICK_STAIRS, Item.getItemFromBlock(Blocks.NETHERRACK), 1); + initRecipe(Blocks.NETHER_BRICK_FENCE, Item.getItemFromBlock(Blocks.NETHERRACK), 1); + initRecipe(Blocks.NETHERRACK, Item.getItemFromBlock(Blocks.SOUL_SAND), 1); + initRecipe(Blocks.SOUL_SAND, Items.GUNPOWDER, 5); + initRecipe(Blocks.SLIME_BLOCK, Items.SLIME_BALL, 10); + initRecipe(Blocks.OBSIDIAN, Items.FLINT, 10); + initRecipe(Blocks.PRISMARINE, Items.PRISMARINE_SHARD, 10); + initRecipe(Blocks.SEA_LANTERN, Items.PRISMARINE_CRYSTALS, 10); + } + + private void initRecipe(Block block, Item item, int amount) { + addGrindingRecipe( + new ItemStack(Item.getItemFromBlock(block), 1, 32767), + new ItemStack(item, amount), 0.7f + ); } public void addGrindingRecipe(ItemStack parItemStackIn, ItemStack parItemStackOut, @@ -52,9 +59,6 @@ public void addGrindingRecipe(ItemStack parItemStackIn, ItemStack parItemStackOu experienceList.put(parItemStackOut, Float.valueOf(parExperience)); } - /** - * Returns the grinding result of an item. - */ public ItemStack getGrindingResult(ItemStack parItemStack) { Iterator iterator = grindingList.entrySet().iterator(); Entry entry; @@ -78,17 +82,13 @@ private boolean areItemStacksEqual(ItemStack parItemStack1, ItemStack parItemSta ); } - public Map getGrindingList() { - return grindingList; - } - public float getGrindingExperience(ItemStack parItemStack) { Iterator iterator = experienceList.entrySet().iterator(); Entry entry; do { if(!iterator.hasNext()) { - return 0.0f; + return 0; } entry = (Entry) iterator.next(); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java index 30acf89..35004f6 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java @@ -8,7 +8,7 @@ import net.minecraft.util.math.MathHelper; public class SlotGrinderOutput extends Slot { - /** The player that is using the GUI where this slot resides. */ + private final EntityPlayer thePlayer; private int numGrinderOutput; @@ -18,24 +18,16 @@ public SlotGrinderOutput(EntityPlayer parPlayer, IInventory parIInventory, thePlayer = parPlayer; } - /** - * Check if the stack is a valid item for this slot. . - */ @Override public boolean isItemValid(ItemStack stack) { - return false; // can't place anything into it + return false; } - /** - * Decrease the size of the stack in slot by the amount of the int arg. Returns the new - * stack. - */ @Override public ItemStack decrStackSize(int parAmount) { if(getHasStack()) { numGrinderOutput += Math.min(parAmount, getStack().stackSize); } - return super.decrStackSize(parAmount); } @@ -64,11 +56,11 @@ protected void onCrafting(ItemStack parItemStack) { int expEarned = numGrinderOutput; float expFactor = GrinderRecipes.instance().getGrindingExperience(parItemStack); - if(expFactor == 0.0f) { + if(expFactor == 0) { expEarned = 0; } - else if (expFactor < 1.0f) { - int possibleExpEarned = MathHelper.floor_float(expEarned * expFactor); + else if(expFactor < 1.0f) { + int possibleExpEarned = MathHelper.floor_float(expEarned*expFactor); if(possibleExpEarned < MathHelper.ceiling_float_int(expEarned*expFactor) && Math.random() < expEarned*expFactor - possibleExpEarned) { @@ -86,8 +78,8 @@ else if (expFactor < 1.0f) { thePlayer.worldObj.spawnEntityInWorld(new EntityXPOrb( thePlayer.worldObj, thePlayer.posX, - thePlayer.posY + 0.5D, - thePlayer.posZ + 0.5D, + thePlayer.posY + 0.5d, + thePlayer.posZ + 0.5d, expInOrb )); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index c46b65f..422bd9a 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -5,7 +5,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; -import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -15,8 +14,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.EnumFacing; import net.minecraft.world.World; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; public class TileEntityGrinder extends TileEntityLockable implements ITickable, ISidedInventory { @@ -24,49 +21,33 @@ public class TileEntityGrinder extends TileEntityLockable public enum slotEnum { INPUT_SLOT, OUTPUT_SLOT } + private static final int[] slotsTop = new int[] { slotEnum.INPUT_SLOT.ordinal() }; private static final int[] slotsBottom = new int[] { slotEnum.OUTPUT_SLOT.ordinal() }; private static final int[] slotsSides = new int[] {}; - /** The ItemStacks that hold the items currently being used in the grinder */ private ItemStack[] grinderItemStackArray = new ItemStack[2]; - /** The number of ticks that the grinder will keep grinding */ private int timeCanGrind; - /** The number of ticks that a fresh copy of the currently-grinding item would keep the grinder grinding for */ private int currentItemGrindTime; private int ticksGrindingItemSoFar; private int ticksPerItem; private String grinderCustomName; - /** - * This controls whether the tile entity gets replaced whenever the block state is changed. - * Normally only want this when block actually is replaced. - */ @Override public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) { return oldState.getBlock() != newSate.getBlock(); } - /** - * Returns the number of slots in the inventory. - */ @Override public int getSizeInventory() { return grinderItemStackArray.length; } - /** - * Returns the stack in slot i - */ @Override public ItemStack getStackInSlot(int index) { return grinderItemStackArray[index]; } - /** - * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a - * new stack. - */ @Override public ItemStack decrStackSize(int index, int count) { if(grinderItemStackArray[index] != null) { @@ -91,6 +72,7 @@ public ItemStack decrStackSize(int index, int count) { return null; } } + /** * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem - * like when you close a workbench GUI. @@ -107,12 +89,8 @@ public ItemStack removeStackFromSlot(int index) { } } - /** - * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections). - */ @Override public void setInventorySlotContents(int index, ItemStack stack) { - boolean isSameItemStackAlreadyInSlot = stack != null && stack.isItemEqual(grinderItemStackArray[index]) && ItemStack.areItemStackTagsEqual(stack, grinderItemStackArray[index]); @@ -140,9 +118,6 @@ public String getName() { } } - /** - * Returns true if this thing is named - */ @Override public boolean hasCustomName() { return grinderCustomName != null && grinderCustomName.length() > 0; @@ -202,30 +177,19 @@ public NBTTagCompound writeToNBT(NBTTagCompound compound) { return compound; } - /** - * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't - * this more of a set than a get?* - */ @Override public int getInventoryStackLimit() { return 64; } - /** - * Grinder is grinding - */ - public boolean grindingSomething() { - return true; - } - - // this function indicates whether container texture should be drawn - @SideOnly(Side.CLIENT) - public static boolean func_174903_a(IInventory parIInventory) { + private boolean grindingSomething() { + // TODO(TT): actually determine if we're grinding something return true; } @Override public void update() { + // TODO(TT): fix this method. it actually makes no sense boolean hasBeenGrinding = grindingSomething(); boolean changedGrindingState = false; @@ -234,39 +198,19 @@ public void update() { } if(!worldObj.isRemote) { - // if something in input slot - if(grinderItemStackArray[slotEnum.INPUT_SLOT.ordinal()] != null) { - // start grinding - if(!grindingSomething() && canGrind()) { - timeCanGrind = 150; - - if(grindingSomething()) { - changedGrindingState = true; - } + if(inputSlotIsOccupied()) { + if(shouldStartGrinding()) { + changedGrindingState = startGrinding(changedGrindingState); } - // continue grinding - if(grindingSomething() && canGrind()) { - ++ticksGrindingItemSoFar; - - // check if completed grinding an item - if(ticksGrindingItemSoFar == ticksPerItem) { - ticksGrindingItemSoFar = 0; - ticksPerItem = timeToGrindOneItem(grinderItemStackArray[0]); - grindItem(); - changedGrindingState = true; - } + if(shouldContinueGrinding()) { + changedGrindingState = continueGrinding(changedGrindingState); } else { ticksGrindingItemSoFar = 0; } } - - // started or stopped grinding, update block to change to active or inactive model - if(hasBeenGrinding != grindingSomething()) { - // the isGrinding() value may have changed due to call to grindItem() earlier - changedGrindingState = true; - } + changedGrindingState = updateGrindingState(hasBeenGrinding, changedGrindingState); } if(changedGrindingState) { @@ -274,43 +218,83 @@ public void update() { } } - public int timeToGrindOneItem(ItemStack parItemStack) { + private boolean updateGrindingState(boolean hasBeenGrinding, boolean changedGrindingState) { + // started or stopped grinding, update block to change to active or inactive model + if(hasBeenGrinding != grindingSomething()) { + // the isGrinding() value may have changed due to call to grindItem() earlier + changedGrindingState = true; + } + return changedGrindingState; + } + + private boolean shouldContinueGrinding() { + return grindingSomething() && canGrind(); + } + + private boolean shouldStartGrinding() { + return !grindingSomething() && canGrind(); + } + + private boolean continueGrinding(boolean changedGrindingState) { + ++ticksGrindingItemSoFar; + + // check if completed grinding an item + if(grindingCompleted()) { + ticksGrindingItemSoFar = 0; + ticksPerItem = timeToGrindOneItem(grinderItemStackArray[0]); + grindItem(); + changedGrindingState = true; + } + return changedGrindingState; + } + + private boolean grindingCompleted() { + return ticksGrindingItemSoFar == ticksPerItem; + } + + private boolean startGrinding(boolean changedGrindingState) { + timeCanGrind = 150; + + if(grindingSomething()) { + changedGrindingState = true; + } + return changedGrindingState; + } + + private boolean inputSlotIsOccupied() { + return grinderItemStackArray[slotEnum.INPUT_SLOT.ordinal()] != null; + } + + private int timeToGrindOneItem(ItemStack parItemStack) { + // TODO(TT): check types of item stack to determine grind time return 200; } - /** - * Returns true if the grinder can grind an item, i.e. has a source item, destination stack isn't full, etc. - */ private boolean canGrind() { int inputSlot = slotEnum.INPUT_SLOT.ordinal(); int outputSlot = slotEnum.OUTPUT_SLOT.ordinal(); ItemStack inputStack = grinderItemStackArray[inputSlot]; ItemStack outputStack = grinderItemStackArray[outputSlot]; - // if nothing in input slot if(inputStack == null) { return false; } - else { // check if it has a grinding recipe + else { ItemStack itemStackToOutput = GrinderRecipes.instance().getGrindingResult(inputStack); if(itemStackToOutput == null) { - return false; // no valid recipe for grinding this item + return false; } if(outputStack == null) { - return true; // output slot is empty + return true; } if(!outputStack.isItemEqual(itemStackToOutput)) { - return false; // output slot has different item occupying it + return false; } - // check if output slot is full int result = outputStack.stackSize + itemStackToOutput.stackSize; return result <= getInventoryStackLimit() && result <= outputStack.getMaxStackSize(); } } - /** - * Turn one item from the grinder source stack into the appropriate grinded item in the grinder result stack - */ public void grindItem() { if(canGrind()) { int inputSlot = slotEnum.INPUT_SLOT.ordinal(); @@ -336,9 +320,6 @@ else if(outputStack.getItem() == itemstack.getItem()) { } } - /** - * Do not make give this method the name canInteractWith because it clashes with Container - */ @Override public boolean isUseableByPlayer(EntityPlayer playerIn) { if(worldObj.getTileEntity(pos) != this) { @@ -380,19 +361,11 @@ public int[] getSlotsForFace(EnumFacing side) { } } - /** - * Returns true if automation can insert the given item in the given slot from the given side. Args: slot, item, - * side - */ @Override public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) { return isItemValidForSlot(index, itemStackIn); } - /** - * Returns true if automation can extract the given item in the given slot from the given side. Args: slot, item, - * side - */ @Override public boolean canExtractItem(int parSlotIndex, ItemStack parStack, EnumFacing parFacing) { return true; @@ -405,7 +378,6 @@ public String getGuiID() { @Override public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) { - System.out.println("DEBUG: TileEntityGrinder createContainer()"); return new ContainerGrinder(playerInventory, this); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java index 14d7b21..668d83b 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java @@ -25,29 +25,24 @@ public GuiGrinder(InventoryPlayer parInventoryPlayer, IInventory parInventoryGri tileGrinder = parInventoryGrinder; } - /** - * Draw the foreground layer for the GuiContainer (everything in front of the items). Args : mouseX, mouseY - */ @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { String s = tileGrinder.getDisplayName().getUnformattedText(); + int color = 4210752; fontRendererObj.drawString( s, xSize/2 - fontRendererObj.getStringWidth(s)/2, 6, - 4210752 + color ); fontRendererObj.drawString( inventoryPlayer.getDisplayName().getUnformattedText(), 8, ySize - 96 + 2, - 4210752 + color ); } - /** - * Args : renderPartialTicks, mouseX, mouseY - */ @Override protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java index a1bae47..9c6b5a6 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiHandler.java @@ -11,18 +11,12 @@ public class GuiHandler implements IGuiHandler { - public GuiHandler() { - System.out.println("DEBUG: GuiHandler created"); - } - @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); - System.out.println("DEBUG: getServerGuiElement() called"); - if(tileEntity != null) { - if(ID == CustomMod.GUI_ENUM.GRINDER.ordinal()) { + if(isGrinderID(ID)) { return new ContainerGrinder(player.inventory, (IInventory) tileEntity); } } @@ -34,14 +28,17 @@ public Object getServerGuiElement(int ID, EntityPlayer player, World world, int public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); - System.out.println("DEBUG: getClientGuiElement() called"); - if(tileEntity != null) { - if(ID == CustomMod.GUI_ENUM.GRINDER.ordinal()) { + if(isGrinderID(ID)) { return new GuiGrinder(player.inventory, (IInventory) tileEntity); } } + return null; } + private boolean isGrinderID(int ID) { + return ID == CustomMod.GUI.GRINDER.ordinal(); + } + } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java b/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java index c10940d..f512b1f 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/proxy/CommonProxy.java @@ -12,23 +12,13 @@ public class CommonProxy { - public void preInit(FMLPreInitializationEvent event) { - // TODO - System.out.println("DEBUG: Server proxy preInit()"); - } + public void preInit(FMLPreInitializationEvent event) {} public void init(FMLInitializationEvent event) { - NetworkRegistry.INSTANCE.registerGuiHandler( - CustomMod.instance, - new GuiHandler() - ); - System.out.println("DEBUG: Server proxy init()"); + registerGuiHandlers(); } - public void postInit(FMLPostInitializationEvent event) { - // TODO - System.out.println("DEBUG: Server proxy postInit()"); - } + public void postInit(FMLPostInitializationEvent event) {} public void registerItemRenderer(Item item, int meta, String id) { ModelResourceLocation location = new ModelResourceLocation( @@ -38,4 +28,11 @@ public void registerItemRenderer(Item item, int meta, String id) { ModelLoader.setCustomModelResourceLocation(item, meta, location); } + private void registerGuiHandlers() { + NetworkRegistry.INSTANCE.registerGuiHandler( + CustomMod.instance, + new GuiHandler() + ); + } + } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java b/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java index e0b3857..e59c28b 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/tileentities/BlockContainerTileEntity.java @@ -17,6 +17,7 @@ public abstract class BlockContainerTileEntity public BlockContainerTileEntity(Material material, String name) { super(material); + setUnlocalizedName(name); setRegistryName(name); setCreativeTab(CustomMod.tab); @@ -24,13 +25,8 @@ public BlockContainerTileEntity(Material material, String name) { public abstract Class getTileEntityClass(); - public TE getTileEntity(IBlockAccess world, BlockPos pos) { - return (TE) world.getTileEntity(pos); - } - @Override public boolean hasTileEntity(IBlockState state) { - System.out.println("DEBUG: hasTileEntity"); return true; } From c3c6d4e1c7cb5d7f4b339ba4292a80aa1f43b1e8 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 26 Mar 2017 13:31:09 -0500 Subject: [PATCH 05/15] Some refactoring --- .../mod/blocks/grinder/ContainerGrinder.java | 84 ++++++++-------- .../mod/blocks/grinder/GrinderRecipes.java | 95 ++++++++++++------- .../mod/blocks/grinder/SlotGrinderOutput.java | 58 +++++------ .../mod/blocks/grinder/TileEntityGrinder.java | 13 ++- .../minecraft/mod/proxy/ClientProxy.java | 11 +-- .../assets/custommod/lang/en_US.lang | 1 + 6 files changed, 146 insertions(+), 116 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index 7894b27..df8091b 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -19,45 +19,11 @@ public class ContainerGrinder extends Container { private int timeCanGrind; public ContainerGrinder(InventoryPlayer parInventoryPlayer, IInventory parIInventory) { - // TODO(TT): Break this method up. Too many lines. tileGrinder = parIInventory; sizeInventory = tileGrinder.getSizeInventory(); - addSlotToContainer(new Slot( - tileGrinder, - TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal(), - 56, - 35 - )); - addSlotToContainer(new SlotGrinderOutput( - parInventoryPlayer.player, - tileGrinder, - TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal(), - 116, - 35 - )); - - // add player inventory slots - // note that the slot numbers are within the player inventory so can be same as the tile entity inventory - for(int i = 0; i < 3; ++i) { - for(int j = 0; j < 9; ++j) { - addSlotToContainer(new Slot( - parInventoryPlayer, - j + i * 9 + 9, - 8 + j * 18, - 84 + i * 18 - )); - } - } - - // add hotbar slots - for(int i = 0; i < 9; ++i) { - addSlotToContainer(new Slot( - parInventoryPlayer, - i, - 8 + i * 18, - 142 - )); - } + addContainerSlots(parInventoryPlayer); + addPlayerInventorySlots(parInventoryPlayer); + addHotbarSlots(parInventoryPlayer); } /** @@ -126,7 +92,7 @@ public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) { } else if(slotIndex != TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal()) { // check if there is a grinding recipe for the stack - if(GrinderRecipes.instance().getGrindingResult(itemStack2) != null) { + if(GrinderRecipes.getGrindingResult(itemStack2) != null) { if(!mergeItemStack(itemStack2, 0, 1, false)) { return null; } @@ -178,4 +144,46 @@ else if(!mergeItemStack( return itemStack1; } + private void addContainerSlots(InventoryPlayer parInventoryPlayer) { + addSlotToContainer(new Slot( + tileGrinder, + TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal(), + 56, + 35 + )); + addSlotToContainer(new SlotGrinderOutput( + parInventoryPlayer.player, + tileGrinder, + TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal(), + 116, + 35 + )); + } + + private void addPlayerInventorySlots(InventoryPlayer parInventoryPlayer) { + // note that the slot numbers are within the player inventory + // so can they be the same those for the tile entity inventory + for(int i = 0; i < 3; ++i) { + for(int j = 0; j < 9; ++j) { + addSlotToContainer(new Slot( + parInventoryPlayer, + j + i * 9 + 9, + 8 + j * 18, + 84 + i * 18 + )); + } + } + } + + private void addHotbarSlots(InventoryPlayer parInventoryPlayer) { + for(int i = 0; i < 9; ++i) { + addSlotToContainer(new Slot( + parInventoryPlayer, + i, + 8 + i * 18, + 142 + )); + } + } + } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java index 93a4548..bb696ca 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java @@ -1,11 +1,10 @@ package com.quantumindustries.minecraft.mod.blocks.grinder; +import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; -import com.google.common.collect.Maps; - import net.minecraft.block.Block; import net.minecraft.init.Blocks; import net.minecraft.init.Items; @@ -14,15 +13,15 @@ public class GrinderRecipes { - private static final GrinderRecipes grindingBase = new GrinderRecipes(); - private final Map grindingList = Maps.newHashMap(); - private final Map experienceList = Maps.newHashMap(); + private static Map grindingList; + private static Map experienceList; + private static int metadata; - public static GrinderRecipes instance() { - return grindingBase; - } + public static void init() { + grindingList = new HashMap(); + experienceList = new HashMap(); + metadata = 32767; - private GrinderRecipes() { initRecipe(Blocks.STONEBRICK, Item.getItemFromBlock(Blocks.GRAVEL), 1); initRecipe(Blocks.STONE_SLAB, Item.getItemFromBlock(Blocks.GRAVEL), 1); initRecipe(Blocks.STONE_SLAB2, Item.getItemFromBlock(Blocks.GRAVEL), 1); @@ -46,56 +45,80 @@ private GrinderRecipes() { initRecipe(Blocks.SEA_LANTERN, Items.PRISMARINE_CRYSTALS, 10); } - private void initRecipe(Block block, Item item, int amount) { - addGrindingRecipe( - new ItemStack(Item.getItemFromBlock(block), 1, 32767), - new ItemStack(item, amount), 0.7f - ); + private GrinderRecipes() { + metadata = 32767; + + initRecipe(Blocks.STONEBRICK, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.STONE_SLAB, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.STONE_SLAB2, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.SANDSTONE_STAIRS, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.STONE, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.GRAVEL, Item.getItemFromBlock(Blocks.SAND), 1); + initRecipe(Blocks.SANDSTONE, Item.getItemFromBlock(Blocks.SAND), 1); + initRecipe(Blocks.GLASS, Item.getItemFromBlock(Blocks.SAND), 1); + initRecipe(Blocks.BRICK_BLOCK, Item.getItemFromBlock(Blocks.GRAVEL), 1); + initRecipe(Blocks.PLANKS, Items.PAPER, 10); + initRecipe(Blocks.LOG, Items.PAPER, 1); + initRecipe(Blocks.LOG2, Items.PAPER, 1); + initRecipe(Blocks.NETHER_BRICK, Item.getItemFromBlock(Blocks.NETHERRACK), 1); + initRecipe(Blocks.NETHER_BRICK_STAIRS, Item.getItemFromBlock(Blocks.NETHERRACK), 1); + initRecipe(Blocks.NETHER_BRICK_FENCE, Item.getItemFromBlock(Blocks.NETHERRACK), 1); + initRecipe(Blocks.NETHERRACK, Item.getItemFromBlock(Blocks.SOUL_SAND), 1); + initRecipe(Blocks.SOUL_SAND, Items.GUNPOWDER, 5); + initRecipe(Blocks.SLIME_BLOCK, Items.SLIME_BALL, 10); + initRecipe(Blocks.OBSIDIAN, Items.FLINT, 10); + initRecipe(Blocks.PRISMARINE, Items.PRISMARINE_SHARD, 10); + initRecipe(Blocks.SEA_LANTERN, Items.PRISMARINE_CRYSTALS, 10); } - public void addGrindingRecipe(ItemStack parItemStackIn, ItemStack parItemStackOut, + public static void addGrindingRecipe(ItemStack parItemStackIn, ItemStack parItemStackOut, float parExperience) { grindingList.put(parItemStackIn, parItemStackOut); experienceList.put(parItemStackOut, Float.valueOf(parExperience)); } - public ItemStack getGrindingResult(ItemStack parItemStack) { - Iterator iterator = grindingList.entrySet().iterator(); - Entry entry; + public static ItemStack getGrindingResult(ItemStack parItemStack) { + Iterator> iterator = grindingList.entrySet().iterator(); + Entry entry; do { if(!iterator.hasNext()) { return null; } - - entry = (Entry) iterator.next(); + entry = iterator.next(); } - while(!areItemStacksEqual(parItemStack, (ItemStack)entry.getKey())); - - return (ItemStack) entry.getValue(); - } + while(!areItemStacksEqual(parItemStack, entry.getKey())); - private boolean areItemStacksEqual(ItemStack parItemStack1, ItemStack parItemStack2) { - return parItemStack2.getItem() == parItemStack1.getItem() && - (parItemStack2.getMetadata() == 32767 || - parItemStack2.getMetadata() == parItemStack1.getMetadata() - ); + return entry.getValue(); } - public float getGrindingExperience(ItemStack parItemStack) { - Iterator iterator = experienceList.entrySet().iterator(); - Entry entry; + public static float getGrindingExperience(ItemStack parItemStack) { + Iterator> iterator = experienceList.entrySet().iterator(); + Entry entry; do { if(!iterator.hasNext()) { return 0; } - - entry = (Entry) iterator.next(); + entry = iterator.next(); } - while (!areItemStacksEqual(parItemStack, (ItemStack) entry.getKey())); + while(!areItemStacksEqual(parItemStack, entry.getKey())); + + return entry.getValue(); + } - return ((Float) entry.getValue()).floatValue(); + private static void initRecipe(Block block, Item item, int amount) { + addGrindingRecipe( + new ItemStack(Item.getItemFromBlock(block), 1, metadata), + new ItemStack(item, amount), 0.7f + ); + } + + private static boolean areItemStacksEqual(ItemStack parItemStack1, ItemStack parItemStack2) { + return parItemStack2.getItem() == parItemStack1.getItem() && + (parItemStack2.getMetadata() == metadata || + parItemStack2.getMetadata() == parItemStack1.getMetadata() + ); } } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java index 35004f6..419ea1d 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java @@ -53,39 +53,43 @@ protected void onCrafting(ItemStack parItemStack, int parAmountGround) { @Override protected void onCrafting(ItemStack parItemStack) { if(!thePlayer.worldObj.isRemote) { - int expEarned = numGrinderOutput; - float expFactor = GrinderRecipes.instance().getGrindingExperience(parItemStack); - - if(expFactor == 0) { - expEarned = 0; - } - else if(expFactor < 1.0f) { - int possibleExpEarned = MathHelper.floor_float(expEarned*expFactor); + float expFactor = GrinderRecipes.getGrindingExperience(parItemStack); + int expEarned = getExpEarned(numGrinderOutput, expFactor); + createExperienceOrbs(expEarned); + } + numGrinderOutput = 0; + } - if(possibleExpEarned < MathHelper.ceiling_float_int(expEarned*expFactor) && - Math.random() < expEarned*expFactor - possibleExpEarned) { - ++possibleExpEarned; - } + private int getExpEarned(int expEarned, float expFactor) { + if(expFactor == 0) { + expEarned = 0; + } + else if(expFactor < 1.0f) { + int possibleExpEarned = MathHelper.floor_float(expEarned*expFactor); - expEarned = possibleExpEarned; + if(possibleExpEarned < MathHelper.ceiling_float_int(expEarned*expFactor) && + Math.random() < expEarned*expFactor - possibleExpEarned) { + ++possibleExpEarned; } - // create experience orbs - int expInOrb; - while(expEarned > 0) { - expInOrb = EntityXPOrb.getXPSplit(expEarned); - expEarned -= expInOrb; - thePlayer.worldObj.spawnEntityInWorld(new EntityXPOrb( - thePlayer.worldObj, - thePlayer.posX, - thePlayer.posY + 0.5d, - thePlayer.posZ + 0.5d, - expInOrb - )); - } + expEarned = possibleExpEarned; } + return expEarned; + } - numGrinderOutput = 0; + private void createExperienceOrbs(int expEarned) { + int expInOrb; + while(expEarned > 0) { + expInOrb = EntityXPOrb.getXPSplit(expEarned); + expEarned -= expInOrb; + thePlayer.worldObj.spawnEntityInWorld(new EntityXPOrb( + thePlayer.worldObj, + thePlayer.posX, + thePlayer.posY + 0.5d, + thePlayer.posZ + 0.5d, + expInOrb + )); + } } } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index 422bd9a..7a31dac 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -279,7 +279,7 @@ private boolean canGrind() { return false; } else { - ItemStack itemStackToOutput = GrinderRecipes.instance().getGrindingResult(inputStack); + ItemStack itemStackToOutput = GrinderRecipes.getGrindingResult(inputStack); if(itemStackToOutput == null) { return false; } @@ -302,7 +302,7 @@ public void grindItem() { ItemStack inputStack = grinderItemStackArray[inputSlot]; ItemStack outputStack = grinderItemStackArray[outputSlot]; - ItemStack itemstack = GrinderRecipes.instance().getGrindingResult(inputStack); + ItemStack itemstack = GrinderRecipes.getGrindingResult(inputStack); // check if output slot is empty if(outputStack == null) { @@ -326,11 +326,10 @@ public boolean isUseableByPlayer(EntityPlayer playerIn) { return false; } else { - return playerIn.getDistanceSq( - pos.getX() + 0.5D, - pos.getY() + 0.5D, - pos.getZ() + 0.5D - ) <= 64.0D; + double x = pos.getX() + 0.5d; + double y = pos.getY() + 0.5d; + double z = pos.getZ() + 0.5d; + return playerIn.getDistanceSq(x, y, z) <= 64.0d; } } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/proxy/ClientProxy.java b/src/main/java/com/quantumindustries/minecraft/mod/proxy/ClientProxy.java index e511289..35f5267 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/proxy/ClientProxy.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/proxy/ClientProxy.java @@ -3,6 +3,7 @@ import com.quantumindustries.minecraft.mod.CustomMod; import com.quantumindustries.minecraft.mod.ModWorldGen; import com.quantumindustries.minecraft.mod.blocks.ModBlocks; +import com.quantumindustries.minecraft.mod.blocks.grinder.GrinderRecipes; import com.quantumindustries.minecraft.mod.fluids.ModFluids; import com.quantumindustries.minecraft.mod.items.ModItems; import com.quantumindustries.minecraft.mod.recipes.ModRecipes; @@ -23,9 +24,8 @@ public void preInit(FMLPreInitializationEvent event) { ModFluids.init(); ModItems.init(); ModRecipes.init(); + GrinderRecipes.init(); GameRegistry.registerWorldGenerator(new ModWorldGen(), 3); - - System.out.println("DEBUG: Client proxy preInit()"); } @Override @@ -41,14 +41,9 @@ public void init(FMLInitializationEvent event) { ) ); super.init(event); - - System.out.println("DEBUG: Client proxy init()"); } @Override - public void postInit(FMLPostInitializationEvent event) { - // TODO - System.out.println("DEBUG: Client proxy postInit()"); - } + public void postInit(FMLPostInitializationEvent event) {} } diff --git a/src/main/resources/assets/custommod/lang/en_US.lang b/src/main/resources/assets/custommod/lang/en_US.lang index 2d04365..64aeacb 100644 --- a/src/main/resources/assets/custommod/lang/en_US.lang +++ b/src/main/resources/assets/custommod/lang/en_US.lang @@ -24,6 +24,7 @@ tile.blockNeoCobaltMagnet.name=Neodymium-Cobalt Magnet tile.blockNeoRhodiumMagnet.name=Neodymium-Rhodium Magnet tile.powerAnalyzer.name=Power Analyzer tile.infiniteProducer.name=Infinite Power Block +tile.blockGrinder.name=Grinder # Mod Fluids tile.blockFluidNitrogen.name=Liquid Nitrogen From 98f6e5402d7235bf11814d3956cbc8c35813ccb0 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 26 Mar 2017 14:41:26 -0500 Subject: [PATCH 06/15] Make grinding time customizable --- .../mod/blocks/grinder/GrinderRecipes.java | 81 +++++++------------ .../mod/blocks/grinder/TileEntityGrinder.java | 8 +- 2 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java index bb696ca..f2f697f 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java @@ -15,66 +15,43 @@ public class GrinderRecipes { private static Map grindingList; private static Map experienceList; + private static Map grindingTimes; private static int metadata; public static void init() { grindingList = new HashMap(); experienceList = new HashMap(); + grindingTimes = new HashMap(); metadata = 32767; - initRecipe(Blocks.STONEBRICK, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.STONE_SLAB, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.STONE_SLAB2, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.SANDSTONE_STAIRS, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.STONE, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.GRAVEL, Item.getItemFromBlock(Blocks.SAND), 1); - initRecipe(Blocks.SANDSTONE, Item.getItemFromBlock(Blocks.SAND), 1); - initRecipe(Blocks.GLASS, Item.getItemFromBlock(Blocks.SAND), 1); - initRecipe(Blocks.BRICK_BLOCK, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.PLANKS, Items.PAPER, 10); - initRecipe(Blocks.LOG, Items.PAPER, 1); - initRecipe(Blocks.LOG2, Items.PAPER, 1); - initRecipe(Blocks.NETHER_BRICK, Item.getItemFromBlock(Blocks.NETHERRACK), 1); - initRecipe(Blocks.NETHER_BRICK_STAIRS, Item.getItemFromBlock(Blocks.NETHERRACK), 1); - initRecipe(Blocks.NETHER_BRICK_FENCE, Item.getItemFromBlock(Blocks.NETHERRACK), 1); - initRecipe(Blocks.NETHERRACK, Item.getItemFromBlock(Blocks.SOUL_SAND), 1); - initRecipe(Blocks.SOUL_SAND, Items.GUNPOWDER, 5); - initRecipe(Blocks.SLIME_BLOCK, Items.SLIME_BALL, 10); - initRecipe(Blocks.OBSIDIAN, Items.FLINT, 10); - initRecipe(Blocks.PRISMARINE, Items.PRISMARINE_SHARD, 10); - initRecipe(Blocks.SEA_LANTERN, Items.PRISMARINE_CRYSTALS, 10); - } - - private GrinderRecipes() { - metadata = 32767; - - initRecipe(Blocks.STONEBRICK, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.STONE_SLAB, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.STONE_SLAB2, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.SANDSTONE_STAIRS, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.STONE, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.GRAVEL, Item.getItemFromBlock(Blocks.SAND), 1); - initRecipe(Blocks.SANDSTONE, Item.getItemFromBlock(Blocks.SAND), 1); - initRecipe(Blocks.GLASS, Item.getItemFromBlock(Blocks.SAND), 1); - initRecipe(Blocks.BRICK_BLOCK, Item.getItemFromBlock(Blocks.GRAVEL), 1); - initRecipe(Blocks.PLANKS, Items.PAPER, 10); - initRecipe(Blocks.LOG, Items.PAPER, 1); - initRecipe(Blocks.LOG2, Items.PAPER, 1); - initRecipe(Blocks.NETHER_BRICK, Item.getItemFromBlock(Blocks.NETHERRACK), 1); - initRecipe(Blocks.NETHER_BRICK_STAIRS, Item.getItemFromBlock(Blocks.NETHERRACK), 1); - initRecipe(Blocks.NETHER_BRICK_FENCE, Item.getItemFromBlock(Blocks.NETHERRACK), 1); - initRecipe(Blocks.NETHERRACK, Item.getItemFromBlock(Blocks.SOUL_SAND), 1); - initRecipe(Blocks.SOUL_SAND, Items.GUNPOWDER, 5); - initRecipe(Blocks.SLIME_BLOCK, Items.SLIME_BALL, 10); - initRecipe(Blocks.OBSIDIAN, Items.FLINT, 10); - initRecipe(Blocks.PRISMARINE, Items.PRISMARINE_SHARD, 10); - initRecipe(Blocks.SEA_LANTERN, Items.PRISMARINE_CRYSTALS, 10); + initRecipe(Blocks.STONEBRICK, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); + initRecipe(Blocks.STONE_SLAB, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); + initRecipe(Blocks.STONE_SLAB2, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); + initRecipe(Blocks.SANDSTONE_STAIRS, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); + initRecipe(Blocks.STONE, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); + initRecipe(Blocks.GRAVEL, Item.getItemFromBlock(Blocks.SAND), 1, 200); + initRecipe(Blocks.SANDSTONE, Item.getItemFromBlock(Blocks.SAND), 1, 200); + initRecipe(Blocks.GLASS, Item.getItemFromBlock(Blocks.SAND), 1, 200); + initRecipe(Blocks.BRICK_BLOCK, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); + initRecipe(Blocks.PLANKS, Items.PAPER, 10, 200); + initRecipe(Blocks.LOG, Items.PAPER, 1, 200); + initRecipe(Blocks.LOG2, Items.PAPER, 1, 200); + initRecipe(Blocks.NETHER_BRICK, Item.getItemFromBlock(Blocks.NETHERRACK), 1, 200); + initRecipe(Blocks.NETHER_BRICK_STAIRS, Item.getItemFromBlock(Blocks.NETHERRACK), 1, 200); + initRecipe(Blocks.NETHER_BRICK_FENCE, Item.getItemFromBlock(Blocks.NETHERRACK), 1, 200); + initRecipe(Blocks.NETHERRACK, Item.getItemFromBlock(Blocks.SOUL_SAND), 1, 200); + initRecipe(Blocks.SOUL_SAND, Items.GUNPOWDER, 5, 200); + initRecipe(Blocks.SLIME_BLOCK, Items.SLIME_BALL, 10, 200); + initRecipe(Blocks.OBSIDIAN, Items.FLINT, 10, 200); + initRecipe(Blocks.PRISMARINE, Items.PRISMARINE_SHARD, 10, 200); + initRecipe(Blocks.SEA_LANTERN, Items.PRISMARINE_CRYSTALS, 10, 200); } public static void addGrindingRecipe(ItemStack parItemStackIn, ItemStack parItemStackOut, - float parExperience) { + float parExperience, int time) { grindingList.put(parItemStackIn, parItemStackOut); experienceList.put(parItemStackOut, Float.valueOf(parExperience)); + grindingTimes.put(parItemStackIn.getItem().getUnlocalizedName(), time); } public static ItemStack getGrindingResult(ItemStack parItemStack) { @@ -107,10 +84,14 @@ public static float getGrindingExperience(ItemStack parItemStack) { return entry.getValue(); } - private static void initRecipe(Block block, Item item, int amount) { + public static int getGrindingTime(String name) { + return grindingTimes.get(name); + } + + private static void initRecipe(Block block, Item item, int amount, int time) { addGrindingRecipe( new ItemStack(Item.getItemFromBlock(block), 1, metadata), - new ItemStack(item, amount), 0.7f + new ItemStack(item, amount), 0.7f, time ); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index 7a31dac..f5cbc9f 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -266,8 +266,12 @@ private boolean inputSlotIsOccupied() { } private int timeToGrindOneItem(ItemStack parItemStack) { - // TODO(TT): check types of item stack to determine grind time - return 200; + if(parItemStack != null && parItemStack.getItem() != null) { + return GrinderRecipes.getGrindingTime( + parItemStack.getItem().getUnlocalizedName() + ); + } + return 0; } private boolean canGrind() { From 544979700dcf7a94ed78a396f8f41d2a07f76c7c Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 26 Mar 2017 15:29:43 -0500 Subject: [PATCH 07/15] Refactor grinder update on tick --- .../mod/blocks/grinder/ContainerGrinder.java | 20 ++--- .../mod/blocks/grinder/TileEntityGrinder.java | 77 ++++--------------- .../minecraft/mod/guis/GuiGrinder.java | 4 +- 3 files changed, 22 insertions(+), 79 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index df8091b..3dffba4 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -16,7 +16,6 @@ public class ContainerGrinder extends Container { private final int sizeInventory; private int ticksGrindingItemSoFar; private int ticksPerItem; - private int timeCanGrind; public ContainerGrinder(InventoryPlayer parInventoryPlayer, IInventory parIInventory) { tileGrinder = parIInventory; @@ -33,25 +32,20 @@ public ContainerGrinder(InventoryPlayer parInventoryPlayer, IInventory parIInven public void detectAndSendChanges() { super.detectAndSendChanges(); - for(int i = 0; i < listeners.size(); ++i) { + for(int i = 0; i < listeners.size(); i++) { IContainerListener listener = listeners.get(i); - if(ticksGrindingItemSoFar != tileGrinder.getField(2)) { - listener.sendProgressBarUpdate(this, 2, tileGrinder.getField(2)); - } - - if(timeCanGrind != tileGrinder.getField(0)) { - listener.sendProgressBarUpdate(this, 0, tileGrinder.getField(0)); + if(ticksGrindingItemSoFar != tileGrinder.getField(1)) { + listener.sendProgressBarUpdate(this, 1, tileGrinder.getField(1)); } - if(ticksPerItem != tileGrinder.getField(3)) { - listener.sendProgressBarUpdate(this, 3, tileGrinder.getField(3)); + if(ticksPerItem != tileGrinder.getField(2)) { + listener.sendProgressBarUpdate(this, 2, tileGrinder.getField(2)); } } - ticksGrindingItemSoFar = tileGrinder.getField(2); // tick grinding item so far - timeCanGrind = tileGrinder.getField(0); // time can grind - ticksPerItem = tileGrinder.getField(3); // ticks per item + ticksGrindingItemSoFar = tileGrinder.getField(1); // tick grinding item so far + ticksPerItem = tileGrinder.getField(2); // ticks per item } @Override diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index f5cbc9f..92a85e8 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -26,7 +26,6 @@ public enum slotEnum { private static final int[] slotsBottom = new int[] { slotEnum.OUTPUT_SLOT.ordinal() }; private static final int[] slotsSides = new int[] {}; private ItemStack[] grinderItemStackArray = new ItemStack[2]; - private int timeCanGrind; private int currentItemGrindTime; private int ticksGrindingItemSoFar; private int ticksPerItem; @@ -142,9 +141,8 @@ public void readFromNBT(NBTTagCompound compound) { } } - timeCanGrind = compound.getShort("GrindTime"); - ticksGrindingItemSoFar = compound.getShort("CookTime"); - ticksPerItem = compound.getShort("CookTimeTotal"); + ticksGrindingItemSoFar = compound.getShort("GrindTime"); + ticksPerItem = compound.getShort("GrindTimeTotal"); if(compound.hasKey("CustomName", 8)) { grinderCustomName = compound.getString("CustomName"); @@ -154,9 +152,8 @@ public void readFromNBT(NBTTagCompound compound) { @Override public NBTTagCompound writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); - compound.setShort("GrindTime", (short) timeCanGrind); - compound.setShort("CookTime", (short) ticksGrindingItemSoFar); - compound.setShort("CookTimeTotal", (short) ticksPerItem); + compound.setShort("GrindTime", (short) ticksGrindingItemSoFar); + compound.setShort("GrindTimeTotal", (short) ticksPerItem); NBTTagList nbttaglist = new NBTTagList(); for(int i = 0; i < grinderItemStackArray.length; ++i) { @@ -182,35 +179,19 @@ public int getInventoryStackLimit() { return 64; } - private boolean grindingSomething() { - // TODO(TT): actually determine if we're grinding something - return true; - } - @Override public void update() { - // TODO(TT): fix this method. it actually makes no sense - boolean hasBeenGrinding = grindingSomething(); boolean changedGrindingState = false; - if(grindingSomething()) { - --timeCanGrind; - } - if(!worldObj.isRemote) { if(inputSlotIsOccupied()) { - if(shouldStartGrinding()) { - changedGrindingState = startGrinding(changedGrindingState); - } - - if(shouldContinueGrinding()) { + if(canGrind()) { changedGrindingState = continueGrinding(changedGrindingState); } else { ticksGrindingItemSoFar = 0; } } - changedGrindingState = updateGrindingState(hasBeenGrinding, changedGrindingState); } if(changedGrindingState) { @@ -218,49 +199,21 @@ public void update() { } } - private boolean updateGrindingState(boolean hasBeenGrinding, boolean changedGrindingState) { - // started or stopped grinding, update block to change to active or inactive model - if(hasBeenGrinding != grindingSomething()) { - // the isGrinding() value may have changed due to call to grindItem() earlier - changedGrindingState = true; - } - return changedGrindingState; - } - - private boolean shouldContinueGrinding() { - return grindingSomething() && canGrind(); - } - - private boolean shouldStartGrinding() { - return !grindingSomething() && canGrind(); - } - private boolean continueGrinding(boolean changedGrindingState) { ++ticksGrindingItemSoFar; - - // check if completed grinding an item - if(grindingCompleted()) { + if(currentItemGrindingCompleted()) { ticksGrindingItemSoFar = 0; ticksPerItem = timeToGrindOneItem(grinderItemStackArray[0]); - grindItem(); + grindNextItem(); changedGrindingState = true; } return changedGrindingState; } - private boolean grindingCompleted() { + private boolean currentItemGrindingCompleted() { return ticksGrindingItemSoFar == ticksPerItem; } - private boolean startGrinding(boolean changedGrindingState) { - timeCanGrind = 150; - - if(grindingSomething()) { - changedGrindingState = true; - } - return changedGrindingState; - } - private boolean inputSlotIsOccupied() { return grinderItemStackArray[slotEnum.INPUT_SLOT.ordinal()] != null; } @@ -299,7 +252,7 @@ private boolean canGrind() { } } - public void grindItem() { + public void grindNextItem() { if(canGrind()) { int inputSlot = slotEnum.INPUT_SLOT.ordinal(); int outputSlot = slotEnum.OUTPUT_SLOT.ordinal(); @@ -388,12 +341,10 @@ public Container createContainer(InventoryPlayer playerInventory, EntityPlayer p public int getField(int id) { switch(id) { case 0: - return timeCanGrind; - case 1: return currentItemGrindTime; - case 2: + case 1: return ticksGrindingItemSoFar; - case 3: + case 2: return ticksPerItem; } return 0; @@ -403,12 +354,10 @@ public int getField(int id) { public void setField(int id, int value) { switch(id) { case 0: - timeCanGrind = value; - case 1: currentItemGrindTime = value; - case 2: + case 1: ticksGrindingItemSoFar = value; - case 3: + case 2: ticksPerItem = value; } } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java index 668d83b..9ae4777 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java @@ -63,8 +63,8 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i } private int getProgressLevel(int progressIndicatorPixelWidth) { - int ticksGrindingItemSoFar = tileGrinder.getField(2); - int ticksPerItem = tileGrinder.getField(3); + int ticksGrindingItemSoFar = tileGrinder.getField(1); + int ticksPerItem = tileGrinder.getField(2); if(ticksPerItem != 0 && ticksGrindingItemSoFar != 0) { return ticksGrindingItemSoFar*progressIndicatorPixelWidth/ticksPerItem; From fdc9adc969bdd2ff478aee531c047e2492907aae Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 26 Mar 2017 18:12:07 -0500 Subject: [PATCH 08/15] Fix loading bar bug --- .../mod/blocks/grinder/ContainerGrinder.java | 4 ++-- .../minecraft/mod/blocks/grinder/GrinderRecipes.java | 11 +++++++---- .../mod/blocks/grinder/TileEntityGrinder.java | 3 +++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index 3dffba4..15424f5 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -44,8 +44,8 @@ public void detectAndSendChanges() { } } - ticksGrindingItemSoFar = tileGrinder.getField(1); // tick grinding item so far - ticksPerItem = tileGrinder.getField(2); // ticks per item + ticksGrindingItemSoFar = tileGrinder.getField(1); + ticksPerItem = tileGrinder.getField(2); } @Override diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java index f2f697f..c5a238c 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java @@ -64,7 +64,7 @@ public static ItemStack getGrindingResult(ItemStack parItemStack) { } entry = iterator.next(); } - while(!areItemStacksEqual(parItemStack, entry.getKey())); + while(!itemStacksAreEqual(parItemStack, entry.getKey())); return entry.getValue(); } @@ -79,13 +79,16 @@ public static float getGrindingExperience(ItemStack parItemStack) { } entry = iterator.next(); } - while(!areItemStacksEqual(parItemStack, entry.getKey())); + while(!itemStacksAreEqual(parItemStack, entry.getKey())); return entry.getValue(); } public static int getGrindingTime(String name) { - return grindingTimes.get(name); + if(grindingTimes.get(name) != null) { + return grindingTimes.get(name); + } + return 0; } private static void initRecipe(Block block, Item item, int amount, int time) { @@ -95,7 +98,7 @@ private static void initRecipe(Block block, Item item, int amount, int time) { ); } - private static boolean areItemStacksEqual(ItemStack parItemStack1, ItemStack parItemStack2) { + private static boolean itemStacksAreEqual(ItemStack parItemStack1, ItemStack parItemStack2) { return parItemStack2.getItem() == parItemStack1.getItem() && (parItemStack2.getMetadata() == metadata || parItemStack2.getMetadata() == parItemStack1.getMetadata() diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index 92a85e8..21d18cf 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -355,10 +355,13 @@ public void setField(int id, int value) { switch(id) { case 0: currentItemGrindTime = value; + break; case 1: ticksGrindingItemSoFar = value; + break; case 2: ticksPerItem = value; + break; } } From 78e9f8ebc63c02e61b7247aa5b378bdc532859a3 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 26 Mar 2017 18:13:15 -0500 Subject: [PATCH 09/15] Minor formatting changes --- .../custommod/blockstates/blockGrinder.json | 16 ++++++++++++---- .../custommod/models/block/blockGrinder.json | 3 +-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/resources/assets/custommod/blockstates/blockGrinder.json b/src/main/resources/assets/custommod/blockstates/blockGrinder.json index 5b870be..c2eb319 100644 --- a/src/main/resources/assets/custommod/blockstates/blockGrinder.json +++ b/src/main/resources/assets/custommod/blockstates/blockGrinder.json @@ -1,9 +1,17 @@ { "forge_marker": 1, "variants": { - "facing=north": { "model": "custommod:blockGrinder" }, - "facing=south": { "model": "custommod:blockGrinder", "y": 180 }, - "facing=west": { "model": "custommod:blockGrinder", "y": 270 }, - "facing=east": { "model": "custommod:blockGrinder", "y": 90 } + "facing=north": { + "model": "custommod:blockGrinder" + }, + "facing=south": { + "model": "custommod:blockGrinder", "y": 180 + }, + "facing=west": { + "model": "custommod:blockGrinder", "y": 270 + }, + "facing=east": { + "model": "custommod:blockGrinder", "y": 90 + } } } diff --git a/src/main/resources/assets/custommod/models/block/blockGrinder.json b/src/main/resources/assets/custommod/models/block/blockGrinder.json index 275d616..018f483 100644 --- a/src/main/resources/assets/custommod/models/block/blockGrinder.json +++ b/src/main/resources/assets/custommod/models/block/blockGrinder.json @@ -1,7 +1,6 @@ { "parent": "block/orientable", - "textures": - { + "textures": { "top": "blocks/furnace_top", "front": "blocks/furnace_front_off", "side": "blocks/furnace_side" From 621b14aa4f6ed319607fffface708da3b261ec72 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 26 Mar 2017 20:38:35 -0500 Subject: [PATCH 10/15] Refactor container slot transfer --- .../mod/blocks/grinder/ContainerGrinder.java | 110 ++++++++----- .../mod/blocks/grinder/SlotGrinderOutput.java | 5 +- .../mod/blocks/grinder/TileEntityGrinder.java | 153 +++++++++--------- 3 files changed, 144 insertions(+), 124 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index 15424f5..6502de1 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -64,62 +64,33 @@ public boolean canInteractWith(EntityPlayer playerIn) { */ @Override public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) { - // TODO(TT): Break this method up. Too many lines. ItemStack itemStack1 = null; Slot slot = inventorySlots.get(slotIndex); - if(slot != null && slot.getHasStack()) { + if(isValidSlot(slot)) { ItemStack itemStack2 = slot.getStack(); itemStack1 = itemStack2.copy(); - if(slotIndex == TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal()) { - if(!mergeItemStack( - itemStack2, - sizeInventory, - sizeInventory + 36, - true - )) { + if(isOutputSlot(slotIndex)) { + int start = sizeInventory; + int end = sizeInventory + 36; + if(!mergeItemStack(itemStack2, start, end, true)) { return null; } - slot.onSlotChange(itemStack2, itemStack1); } - else if(slotIndex != TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal()) { - // check if there is a grinding recipe for the stack - if(GrinderRecipes.getGrindingResult(itemStack2) != null) { - if(!mergeItemStack(itemStack2, 0, 1, false)) { - return null; - } - } - else if(slotIndex >= sizeInventory && slotIndex < sizeInventory + 27) { // player inventory slots - if(!mergeItemStack( - itemStack2, - sizeInventory + 27, - sizeInventory + 36, - false - )) { - return null; - } - } - else if(slotIndex >= sizeInventory + 27 && - slotIndex < sizeInventory + 36 && + else { + Indices indices = getMergeIndices(itemStack2, slotIndex); + if(indices != null && !mergeItemStack( itemStack2, - sizeInventory + 1, - sizeInventory + 27, - false - )) { // hotbar slots + indices.start, + indices.end, + false) + ) { return null; } } - else if(!mergeItemStack( - itemStack2, - sizeInventory, - sizeInventory + 36, - false - )) { - return null; - } if(itemStack2.stackSize == 0) { slot.putStack(null); @@ -138,6 +109,10 @@ else if(!mergeItemStack( return itemStack1; } + private boolean isPlayerInventorySlot(int slotIndex) { + return slotIndex >= sizeInventory && slotIndex < sizeInventory + 27; + } + private void addContainerSlots(InventoryPlayer parInventoryPlayer) { addSlotToContainer(new Slot( tileGrinder, @@ -155,8 +130,6 @@ private void addContainerSlots(InventoryPlayer parInventoryPlayer) { } private void addPlayerInventorySlots(InventoryPlayer parInventoryPlayer) { - // note that the slot numbers are within the player inventory - // so can they be the same those for the tile entity inventory for(int i = 0; i < 3; ++i) { for(int j = 0; j < 9; ++j) { addSlotToContainer(new Slot( @@ -180,4 +153,55 @@ private void addHotbarSlots(InventoryPlayer parInventoryPlayer) { } } + private boolean isValidSlot(Slot slot) { + return slot != null && slot.getHasStack(); + } + + private boolean isInputSlot(int slotIndex) { + return slotIndex == TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal(); + } + + private boolean isOutputSlot(int slotIndex) { + return slotIndex == TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal(); + } + + private Indices getMergeIndices(ItemStack itemStack, int slotIndex) { + if(!isInputSlot(slotIndex)) { + if(grindingRecipeExists(itemStack)) { + return new Indices(0, 1); + } + else if(isPlayerInventorySlot(slotIndex)) { + return new Indices(sizeInventory + 27, sizeInventory + 36); + } + else if(isHotbarSlot(slotIndex)) { + return new Indices(sizeInventory + 1, sizeInventory + 27); + } + return null; + } + else { + return new Indices(sizeInventory, sizeInventory + 36); + } + } + + private boolean grindingRecipeExists(ItemStack itemStack2) { + return GrinderRecipes.getGrindingResult(itemStack2) != null; + } + + private boolean isHotbarSlot(int slotIndex) { + return slotIndex >= sizeInventory + 27 && + slotIndex < sizeInventory + 36; + } + + private class Indices { + + public int start; + public int end; + + public Indices(int start, int end) { + this.start = start; + this.end = end; + } + + } + } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java index 419ea1d..2ab7dd3 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java @@ -38,8 +38,7 @@ public void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack) { } /** - * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an - * internal count then calls onCrafting(item). + * The itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood */ @Override protected void onCrafting(ItemStack parItemStack, int parAmountGround) { @@ -48,7 +47,7 @@ protected void onCrafting(ItemStack parItemStack, int parAmountGround) { } /** - * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. + * The itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. */ @Override protected void onCrafting(ItemStack parItemStack) { diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index 21d18cf..dbd7c94 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -59,11 +59,9 @@ public ItemStack decrStackSize(int index, int count) { } else { itemstack = grinderItemStackArray[index].splitStack(count); - if(grinderItemStackArray[index].stackSize == 0) { grinderItemStackArray[index] = null; } - return itemstack; } } @@ -72,10 +70,6 @@ public ItemStack decrStackSize(int index, int count) { } } - /** - * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem - - * like when you close a workbench GUI. - */ @Override public ItemStack removeStackFromSlot(int index) { if(grinderItemStackArray[index] != null) { @@ -101,8 +95,7 @@ public void setInventorySlotContents(int index, ItemStack stack) { // if input slot, reset the grinding timers if(index == slotEnum.INPUT_SLOT.ordinal() && !isSameItemStackAlreadyInSlot) { - ticksPerItem = timeToGrindOneItem(stack); - ticksGrindingItemSoFar = 0; + resetTimers(stack); markDirty(); } } @@ -199,59 +192,6 @@ public void update() { } } - private boolean continueGrinding(boolean changedGrindingState) { - ++ticksGrindingItemSoFar; - if(currentItemGrindingCompleted()) { - ticksGrindingItemSoFar = 0; - ticksPerItem = timeToGrindOneItem(grinderItemStackArray[0]); - grindNextItem(); - changedGrindingState = true; - } - return changedGrindingState; - } - - private boolean currentItemGrindingCompleted() { - return ticksGrindingItemSoFar == ticksPerItem; - } - - private boolean inputSlotIsOccupied() { - return grinderItemStackArray[slotEnum.INPUT_SLOT.ordinal()] != null; - } - - private int timeToGrindOneItem(ItemStack parItemStack) { - if(parItemStack != null && parItemStack.getItem() != null) { - return GrinderRecipes.getGrindingTime( - parItemStack.getItem().getUnlocalizedName() - ); - } - return 0; - } - - private boolean canGrind() { - int inputSlot = slotEnum.INPUT_SLOT.ordinal(); - int outputSlot = slotEnum.OUTPUT_SLOT.ordinal(); - ItemStack inputStack = grinderItemStackArray[inputSlot]; - ItemStack outputStack = grinderItemStackArray[outputSlot]; - if(inputStack == null) { - return false; - } - else { - ItemStack itemStackToOutput = GrinderRecipes.getGrindingResult(inputStack); - if(itemStackToOutput == null) { - return false; - } - if(outputStack == null) { - return true; - } - if(!outputStack.isItemEqual(itemStackToOutput)) { - return false; - } - int result = outputStack.stackSize + itemStackToOutput.stackSize; - return result <= getInventoryStackLimit() && - result <= outputStack.getMaxStackSize(); - } - } - public void grindNextItem() { if(canGrind()) { int inputSlot = slotEnum.INPUT_SLOT.ordinal(); @@ -339,29 +279,28 @@ public Container createContainer(InventoryPlayer playerInventory, EntityPlayer p @Override public int getField(int id) { - switch(id) { - case 0: - return currentItemGrindTime; - case 1: - return ticksGrindingItemSoFar; - case 2: - return ticksPerItem; + if(id == 0) { + return currentItemGrindTime; + } + else if(id == 1) { + return ticksGrindingItemSoFar; + } + else if(id == 2) { + return ticksPerItem; } return 0; } @Override public void setField(int id, int value) { - switch(id) { - case 0: - currentItemGrindTime = value; - break; - case 1: - ticksGrindingItemSoFar = value; - break; - case 2: - ticksPerItem = value; - break; + if(id == 0) { + currentItemGrindTime = value; + } + else if(id == 1) { + ticksGrindingItemSoFar = value; + } + else if(id == 2) { + ticksPerItem = value; } } @@ -377,4 +316,62 @@ public void clear() { } } + private boolean continueGrinding(boolean changedGrindingState) { + ++ticksGrindingItemSoFar; + if(currentItemGrindingCompleted()) { + ticksGrindingItemSoFar = 0; + ticksPerItem = timeToGrindOneItem(grinderItemStackArray[0]); + grindNextItem(); + changedGrindingState = true; + } + return changedGrindingState; + } + + private boolean currentItemGrindingCompleted() { + return ticksGrindingItemSoFar == ticksPerItem; + } + + private boolean inputSlotIsOccupied() { + return grinderItemStackArray[slotEnum.INPUT_SLOT.ordinal()] != null; + } + + private int timeToGrindOneItem(ItemStack parItemStack) { + if(parItemStack != null && parItemStack.getItem() != null) { + return GrinderRecipes.getGrindingTime( + parItemStack.getItem().getUnlocalizedName() + ); + } + return 0; + } + + private boolean canGrind() { + int inputSlot = slotEnum.INPUT_SLOT.ordinal(); + int outputSlot = slotEnum.OUTPUT_SLOT.ordinal(); + ItemStack inputStack = grinderItemStackArray[inputSlot]; + ItemStack outputStack = grinderItemStackArray[outputSlot]; + if(inputStack == null) { + return false; + } + else { + ItemStack itemStackToOutput = GrinderRecipes.getGrindingResult(inputStack); + if(itemStackToOutput == null) { + return false; + } + if(outputStack == null) { + return true; + } + if(!outputStack.isItemEqual(itemStackToOutput)) { + return false; + } + int result = outputStack.stackSize + itemStackToOutput.stackSize; + return result <= getInventoryStackLimit() && + result <= outputStack.getMaxStackSize(); + } + } + + private void resetTimers(ItemStack stack) { + ticksPerItem = timeToGrindOneItem(stack); + ticksGrindingItemSoFar = 0; + } + } From fae8084666662385b0ee70ced15d8e57ef5e5bd2 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 26 Mar 2017 20:54:45 -0500 Subject: [PATCH 11/15] Remove unnecessary comments --- .../quantumindustries/minecraft/mod/blocks/ModBlocks.java | 1 - .../minecraft/mod/blocks/grinder/BlockGrinder.java | 2 +- .../minecraft/mod/blocks/grinder/ContainerGrinder.java | 6 ------ .../minecraft/mod/blocks/grinder/TileEntityGrinder.java | 5 +---- .../mod/blocks/infiniteproducer/BlockInfiniteProducer.java | 1 - 5 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java index 16963cf..167e5e6 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java @@ -66,7 +66,6 @@ private static void initBlockGrinder() { blockGrinder = register(new BlockGrinder()); } - // TODO(TT): Refactor to prevent/reduce instanceof checks private static T register(T block, ItemBlock itemBlock) { GameRegistry.register(block); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java index dc23708..360e2e2 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java @@ -47,7 +47,7 @@ public BlockGrinder() { blockSoundType = SoundType.SNOW; blockParticleGravity = 1.0f; slipperiness = 0.6f; - lightOpacity = 20; // cast a light shadow + lightOpacity = 20; setTickRandomly(false); useNeighborBrightness = false; } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index 6502de1..7d1f813 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -25,9 +25,6 @@ public ContainerGrinder(InventoryPlayer parInventoryPlayer, IInventory parIInven addHotbarSlots(parInventoryPlayer); } - /** - * Looks for changes made in the container, sends them to every listener. - */ @Override public void detectAndSendChanges() { super.detectAndSendChanges(); @@ -59,9 +56,6 @@ public boolean canInteractWith(EntityPlayer playerIn) { return tileGrinder.isUseableByPlayer(playerIn); } - /** - * Take a stack from the specified inventory slot. - */ @Override public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) { ItemStack itemStack1 = null; diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index dbd7c94..bf2bd69 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -93,7 +93,6 @@ public void setInventorySlotContents(int index, ItemStack stack) { stack.stackSize = getInventoryStackLimit(); } - // if input slot, reset the grinding timers if(index == slotEnum.INPUT_SLOT.ordinal() && !isSameItemStackAlreadyInSlot) { resetTimers(stack); markDirty(); @@ -201,12 +200,11 @@ public void grindNextItem() { ItemStack itemstack = GrinderRecipes.getGrindingResult(inputStack); - // check if output slot is empty if(outputStack == null) { grinderItemStackArray[outputSlot] = itemstack.copy(); } else if(outputStack.getItem() == itemstack.getItem()) { - outputStack.stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items + outputStack.stackSize += itemstack.stackSize; } --inputStack.stackSize; @@ -238,7 +236,6 @@ public void closeInventory(EntityPlayer playerIn) {} @Override public boolean isItemValidForSlot(int index, ItemStack stack) { - // can always put things in input (may not grind though) and can't put anything in output return index == slotEnum.INPUT_SLOT.ordinal(); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/infiniteproducer/BlockInfiniteProducer.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/infiniteproducer/BlockInfiniteProducer.java index 83224aa..8fd96ad 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/infiniteproducer/BlockInfiniteProducer.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/infiniteproducer/BlockInfiniteProducer.java @@ -21,7 +21,6 @@ public Class getTileEntityClass() { @Nullable @Override public TileEntityInfiniteProducer createTileEntity(World world, IBlockState state) { - System.out.println("DEBUG: createTileEntity() TileEntityInfiniteProducer"); return new TileEntityInfiniteProducer(); } From ad7ae277f5af633d5f4eee5e2e4518b164f77120 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 9 Apr 2017 11:16:40 -0500 Subject: [PATCH 12/15] Fix parameter names --- .../mod/blocks/grinder/BlockGrinder.java | 48 +++++++++---------- .../mod/blocks/grinder/ContainerGrinder.java | 22 ++++----- .../mod/blocks/grinder/GrinderRecipes.java | 26 +++++----- .../mod/blocks/grinder/SlotGrinderOutput.java | 24 +++++----- .../mod/blocks/grinder/TileEntityGrinder.java | 12 ++--- .../minecraft/mod/guis/GuiGrinder.java | 8 ++-- 6 files changed, 70 insertions(+), 70 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java index 360e2e2..7b0b1d3 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java @@ -58,35 +58,35 @@ public Item getItemDropped(IBlockState state, Random rand, int fortune) { } @Override - public void onBlockAdded(World parWorld, BlockPos parBlockPos, IBlockState parIBlockState) { - if(!parWorld.isRemote) { + public void onBlockAdded(World world, BlockPos blockPos, IBlockState blockState) { + if(!world.isRemote) { EnumFacing enumFacing = getUnblockedFace( - parWorld, - parBlockPos, - parIBlockState + world, + blockPos, + blockState ); - parWorld.setBlockState( - parBlockPos, - parIBlockState.withProperty(FACING, enumFacing), + world.setBlockState( + blockPos, + blockState.withProperty(FACING, enumFacing), 2 ); } } @Override - public boolean onBlockActivated(World parWorld, BlockPos parBlockPos, - IBlockState parIBlockState, EntityPlayer parPlayer, - EnumHand parHand, ItemStack parStack, EnumFacing parFacing, + public boolean onBlockActivated(World world, BlockPos blockPos, + IBlockState blockState, EntityPlayer player, + EnumHand hand, ItemStack stack, EnumFacing facing, float hitX, float hitY, float hitZ) { - if(!parWorld.isRemote) { - parPlayer.openGui( + if(!world.isRemote) { + player.openGui( CustomMod.instance, CustomMod.GUI.GRINDER.ordinal(), - parWorld, - parBlockPos.getX(), - parBlockPos.getY(), - parBlockPos.getZ() + world, + blockPos.getX(), + blockPos.getY(), + blockPos.getZ() ); } @@ -145,7 +145,7 @@ public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World wo } @Override - public EnumBlockRenderType getRenderType(IBlockState parIBlockState) { + public EnumBlockRenderType getRenderType(IBlockState blockState) { return EnumBlockRenderType.MODEL; } @@ -182,13 +182,13 @@ protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] { FACING }); } - private EnumFacing getUnblockedFace(World parWorld, BlockPos parBlockPos, IBlockState parIBlockState) { - IBlockState blockToNorth = parWorld.getBlockState(parBlockPos.north()); - IBlockState blockToSouth = parWorld.getBlockState(parBlockPos.south()); - IBlockState blockToWest = parWorld.getBlockState(parBlockPos.west()); - IBlockState blockToEast = parWorld.getBlockState(parBlockPos.east()); + private EnumFacing getUnblockedFace(World world, BlockPos blockPos, IBlockState blockState) { + IBlockState blockToNorth = world.getBlockState(blockPos.north()); + IBlockState blockToSouth = world.getBlockState(blockPos.south()); + IBlockState blockToWest = world.getBlockState(blockPos.west()); + IBlockState blockToEast = world.getBlockState(blockPos.east()); - EnumFacing enumFacing = parIBlockState.getValue(FACING); + EnumFacing enumFacing = blockState.getValue(FACING); if(shouldFaceSouth(blockToNorth, blockToSouth, enumFacing)) { enumFacing = EnumFacing.SOUTH; diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index 7d1f813..d0ef0a2 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -17,12 +17,12 @@ public class ContainerGrinder extends Container { private int ticksGrindingItemSoFar; private int ticksPerItem; - public ContainerGrinder(InventoryPlayer parInventoryPlayer, IInventory parIInventory) { - tileGrinder = parIInventory; + public ContainerGrinder(InventoryPlayer inventoryPlayer, IInventory inventory) { + tileGrinder = inventory; sizeInventory = tileGrinder.getSizeInventory(); - addContainerSlots(parInventoryPlayer); - addPlayerInventorySlots(parInventoryPlayer); - addHotbarSlots(parInventoryPlayer); + addContainerSlots(inventoryPlayer); + addPlayerInventorySlots(inventoryPlayer); + addHotbarSlots(inventoryPlayer); } @Override @@ -107,7 +107,7 @@ private boolean isPlayerInventorySlot(int slotIndex) { return slotIndex >= sizeInventory && slotIndex < sizeInventory + 27; } - private void addContainerSlots(InventoryPlayer parInventoryPlayer) { + private void addContainerSlots(InventoryPlayer inventoryPlayer) { addSlotToContainer(new Slot( tileGrinder, TileEntityGrinder.slotEnum.INPUT_SLOT.ordinal(), @@ -115,7 +115,7 @@ private void addContainerSlots(InventoryPlayer parInventoryPlayer) { 35 )); addSlotToContainer(new SlotGrinderOutput( - parInventoryPlayer.player, + inventoryPlayer.player, tileGrinder, TileEntityGrinder.slotEnum.OUTPUT_SLOT.ordinal(), 116, @@ -123,11 +123,11 @@ private void addContainerSlots(InventoryPlayer parInventoryPlayer) { )); } - private void addPlayerInventorySlots(InventoryPlayer parInventoryPlayer) { + private void addPlayerInventorySlots(InventoryPlayer inventoryPlayer) { for(int i = 0; i < 3; ++i) { for(int j = 0; j < 9; ++j) { addSlotToContainer(new Slot( - parInventoryPlayer, + inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18 @@ -136,10 +136,10 @@ private void addPlayerInventorySlots(InventoryPlayer parInventoryPlayer) { } } - private void addHotbarSlots(InventoryPlayer parInventoryPlayer) { + private void addHotbarSlots(InventoryPlayer inventoryPlayer) { for(int i = 0; i < 9; ++i) { addSlotToContainer(new Slot( - parInventoryPlayer, + inventoryPlayer, i, 8 + i * 18, 142 diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java index c5a238c..e93b8b9 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java @@ -47,14 +47,14 @@ public static void init() { initRecipe(Blocks.SEA_LANTERN, Items.PRISMARINE_CRYSTALS, 10, 200); } - public static void addGrindingRecipe(ItemStack parItemStackIn, ItemStack parItemStackOut, - float parExperience, int time) { - grindingList.put(parItemStackIn, parItemStackOut); - experienceList.put(parItemStackOut, Float.valueOf(parExperience)); - grindingTimes.put(parItemStackIn.getItem().getUnlocalizedName(), time); + public static void addGrindingRecipe(ItemStack itemStackIn, ItemStack itemStackOut, + float experience, int time) { + grindingList.put(itemStackIn, itemStackOut); + experienceList.put(itemStackOut, Float.valueOf(experience)); + grindingTimes.put(itemStackIn.getItem().getUnlocalizedName(), time); } - public static ItemStack getGrindingResult(ItemStack parItemStack) { + public static ItemStack getGrindingResult(ItemStack itemStack) { Iterator> iterator = grindingList.entrySet().iterator(); Entry entry; @@ -64,12 +64,12 @@ public static ItemStack getGrindingResult(ItemStack parItemStack) { } entry = iterator.next(); } - while(!itemStacksAreEqual(parItemStack, entry.getKey())); + while(!itemStacksAreEqual(itemStack, entry.getKey())); return entry.getValue(); } - public static float getGrindingExperience(ItemStack parItemStack) { + public static float getGrindingExperience(ItemStack itemStack) { Iterator> iterator = experienceList.entrySet().iterator(); Entry entry; @@ -79,7 +79,7 @@ public static float getGrindingExperience(ItemStack parItemStack) { } entry = iterator.next(); } - while(!itemStacksAreEqual(parItemStack, entry.getKey())); + while(!itemStacksAreEqual(itemStack, entry.getKey())); return entry.getValue(); } @@ -98,10 +98,10 @@ private static void initRecipe(Block block, Item item, int amount, int time) { ); } - private static boolean itemStacksAreEqual(ItemStack parItemStack1, ItemStack parItemStack2) { - return parItemStack2.getItem() == parItemStack1.getItem() && - (parItemStack2.getMetadata() == metadata || - parItemStack2.getMetadata() == parItemStack1.getMetadata() + private static boolean itemStacksAreEqual(ItemStack itemStack1, ItemStack itemStack2) { + return itemStack2.getItem() == itemStack1.getItem() && + (itemStack2.getMetadata() == metadata || + itemStack2.getMetadata() == itemStack1.getMetadata() ); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java index 2ab7dd3..f430074 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/SlotGrinderOutput.java @@ -12,10 +12,10 @@ public class SlotGrinderOutput extends Slot { private final EntityPlayer thePlayer; private int numGrinderOutput; - public SlotGrinderOutput(EntityPlayer parPlayer, IInventory parIInventory, - int parSlotIndex, int parXDisplayPosition, int parYDisplayPosition) { - super(parIInventory, parSlotIndex, parXDisplayPosition, parYDisplayPosition); - thePlayer = parPlayer; + public SlotGrinderOutput(EntityPlayer player, IInventory inventory, + int slotIndex, int xDisplayPosition, int yDisplayPosition) { + super(inventory, slotIndex, xDisplayPosition, yDisplayPosition); + thePlayer = player; } @Override @@ -24,11 +24,11 @@ public boolean isItemValid(ItemStack stack) { } @Override - public ItemStack decrStackSize(int parAmount) { + public ItemStack decrStackSize(int amount) { if(getHasStack()) { - numGrinderOutput += Math.min(parAmount, getStack().stackSize); + numGrinderOutput += Math.min(amount, getStack().stackSize); } - return super.decrStackSize(parAmount); + return super.decrStackSize(amount); } @Override @@ -41,18 +41,18 @@ public void onPickupFromSlot(EntityPlayer playerIn, ItemStack stack) { * The itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood */ @Override - protected void onCrafting(ItemStack parItemStack, int parAmountGround) { - numGrinderOutput += parAmountGround; - onCrafting(parItemStack); + protected void onCrafting(ItemStack itemStack, int amountGround) { + numGrinderOutput += amountGround; + onCrafting(itemStack); } /** * The itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. */ @Override - protected void onCrafting(ItemStack parItemStack) { + protected void onCrafting(ItemStack itemStack) { if(!thePlayer.worldObj.isRemote) { - float expFactor = GrinderRecipes.getGrindingExperience(parItemStack); + float expFactor = GrinderRecipes.getGrindingExperience(itemStack); int expEarned = getExpEarned(numGrinderOutput, expFactor); createExperienceOrbs(expEarned); } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index bf2bd69..2008d78 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -114,8 +114,8 @@ public boolean hasCustomName() { return grinderCustomName != null && grinderCustomName.length() > 0; } - public void setCustomInventoryName(String parCustomName) { - grinderCustomName = parCustomName; + public void setCustomInventoryName(String customName) { + grinderCustomName = customName; } @Override @@ -260,7 +260,7 @@ public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direct } @Override - public boolean canExtractItem(int parSlotIndex, ItemStack parStack, EnumFacing parFacing) { + public boolean canExtractItem(int slotIndex, ItemStack stack, EnumFacing facing) { return true; } @@ -332,10 +332,10 @@ private boolean inputSlotIsOccupied() { return grinderItemStackArray[slotEnum.INPUT_SLOT.ordinal()] != null; } - private int timeToGrindOneItem(ItemStack parItemStack) { - if(parItemStack != null && parItemStack.getItem() != null) { + private int timeToGrindOneItem(ItemStack itemStack) { + if(itemStack != null && itemStack.getItem() != null) { return GrinderRecipes.getGrindingTime( - parItemStack.getItem().getUnlocalizedName() + itemStack.getItem().getUnlocalizedName() ); } return 0; diff --git a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java index 9ae4777..15b5d27 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/guis/GuiGrinder.java @@ -19,10 +19,10 @@ public class GuiGrinder extends GuiContainer { private final InventoryPlayer inventoryPlayer; private final IInventory tileGrinder; - public GuiGrinder(InventoryPlayer parInventoryPlayer, IInventory parInventoryGrinder) { - super(new ContainerGrinder(parInventoryPlayer, parInventoryGrinder)); - inventoryPlayer = parInventoryPlayer; - tileGrinder = parInventoryGrinder; + public GuiGrinder(InventoryPlayer inventoryPlayer, IInventory inventoryGrinder) { + super(new ContainerGrinder(inventoryPlayer, inventoryGrinder)); + this.inventoryPlayer = inventoryPlayer; + tileGrinder = inventoryGrinder; } @Override From 309eb8311baa3b4a4b68a01592374e5df2a1fa60 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 9 Apr 2017 11:22:25 -0500 Subject: [PATCH 13/15] Remove unused variable --- .../minecraft/mod/blocks/grinder/BlockGrinder.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java index 7b0b1d3..6591400 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/BlockGrinder.java @@ -33,7 +33,6 @@ public class BlockGrinder extends BlockContainerTileEntity { public static final PropertyDirection FACING = PropertyDirection.create( "facing", EnumFacing.Plane.HORIZONTAL ); - private final boolean isGrinding; private boolean hasTileEntity; public BlockGrinder() { @@ -42,7 +41,6 @@ public BlockGrinder() { FACING, EnumFacing.NORTH )); - isGrinding = true; hasTileEntity = false; blockSoundType = SoundType.SNOW; blockParticleGravity = 1.0f; From 5b1fadaf8c4429a5c9a1730036edcd9423f831d1 Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 9 Apr 2017 11:54:40 -0500 Subject: [PATCH 14/15] Minor refactoring --- .../minecraft/mod/blocks/grinder/TileEntityGrinder.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index 2008d78..7de68dc 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -22,9 +22,6 @@ public enum slotEnum { INPUT_SLOT, OUTPUT_SLOT } - private static final int[] slotsTop = new int[] { slotEnum.INPUT_SLOT.ordinal() }; - private static final int[] slotsBottom = new int[] { slotEnum.OUTPUT_SLOT.ordinal() }; - private static final int[] slotsSides = new int[] {}; private ItemStack[] grinderItemStackArray = new ItemStack[2]; private int currentItemGrindTime; private int ticksGrindingItemSoFar; @@ -242,14 +239,14 @@ public boolean isItemValidForSlot(int index, ItemStack stack) { @Override public int[] getSlotsForFace(EnumFacing side) { if(side == EnumFacing.DOWN) { - return slotsBottom; + return new int[] { slotEnum.OUTPUT_SLOT.ordinal() }; } else { if(side == EnumFacing.UP) { - return slotsTop; + return new int[] { slotEnum.INPUT_SLOT.ordinal() }; } else { - return slotsSides; + return new int[] {}; } } } From 2641b657cb688a5a20cbaa643601f45fec42296c Mon Sep 17 00:00:00 2001 From: tanvirt Date: Sun, 9 Apr 2017 13:51:04 -0500 Subject: [PATCH 15/15] Removed ISidedInventory dependency --- .../minecraft/mod/blocks/ModBlocks.java | 6 +--- .../mod/blocks/grinder/ContainerGrinder.java | 2 -- .../mod/blocks/grinder/GrinderRecipes.java | 2 ++ .../mod/blocks/grinder/TileEntityGrinder.java | 29 +------------------ 4 files changed, 4 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java index 167e5e6..4db5c79 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/ModBlocks.java @@ -38,7 +38,7 @@ public static void init() { register(new BlockInfiniteProducer()); register(new BlockPowerAnalyzer()); - initBlockGrinder(); + blockGrinder = register(new BlockGrinder()); } private static void initMagnetBlocks() { @@ -62,10 +62,6 @@ private static void initOreBlocks() { blockRhodium = register(new BlockOre("blockRhodium", "blockRhodium", 3f, 5f)); } - private static void initBlockGrinder() { - blockGrinder = register(new BlockGrinder()); - } - private static T register(T block, ItemBlock itemBlock) { GameRegistry.register(block); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java index d0ef0a2..2738230 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/ContainerGrinder.java @@ -187,7 +187,6 @@ private boolean isHotbarSlot(int slotIndex) { } private class Indices { - public int start; public int end; @@ -195,7 +194,6 @@ public Indices(int start, int end) { this.start = start; this.end = end; } - } } diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java index e93b8b9..648264d 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/GrinderRecipes.java @@ -19,11 +19,13 @@ public class GrinderRecipes { private static int metadata; public static void init() { + // TODO(TT): collapse all of these maps into a single map grindingList = new HashMap(); experienceList = new HashMap(); grindingTimes = new HashMap(); metadata = 32767; + // TODO(TT): remove recipes that are not intended to be used initRecipe(Blocks.STONEBRICK, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); initRecipe(Blocks.STONE_SLAB, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); initRecipe(Blocks.STONE_SLAB2, Item.getItemFromBlock(Blocks.GRAVEL), 1, 200); diff --git a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java index 7de68dc..14cfd46 100644 --- a/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java +++ b/src/main/java/com/quantumindustries/minecraft/mod/blocks/grinder/TileEntityGrinder.java @@ -5,18 +5,16 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; -import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraft.util.ITickable; import net.minecraft.tileentity.TileEntityLockable; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.EnumFacing; import net.minecraft.world.World; public class TileEntityGrinder extends TileEntityLockable - implements ITickable, ISidedInventory { + implements ITickable { public enum slotEnum { INPUT_SLOT, OUTPUT_SLOT @@ -236,31 +234,6 @@ public boolean isItemValidForSlot(int index, ItemStack stack) { return index == slotEnum.INPUT_SLOT.ordinal(); } - @Override - public int[] getSlotsForFace(EnumFacing side) { - if(side == EnumFacing.DOWN) { - return new int[] { slotEnum.OUTPUT_SLOT.ordinal() }; - } - else { - if(side == EnumFacing.UP) { - return new int[] { slotEnum.INPUT_SLOT.ordinal() }; - } - else { - return new int[] {}; - } - } - } - - @Override - public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) { - return isItemValidForSlot(index, itemStackIn); - } - - @Override - public boolean canExtractItem(int slotIndex, ItemStack stack, EnumFacing facing) { - return true; - } - @Override public String getGuiID() { return CustomMod.MODID + ":blockGrinder";