From ef506e57a9d1444e1254cc841aa266fd6e89bb26 Mon Sep 17 00:00:00 2001 From: Cricket Date: Tue, 6 Jan 2015 20:29:22 -0500 Subject: [PATCH] Chisel left click redone, Thanks tterrag --- .../java/com/cricketcraft/chisel/Chisel.java | 54 ++++- .../cricketcraft/chisel/ChiselLeftClick.java | 186 ------------------ .../chisel/block/BlockPresent.java | 45 ++--- .../chisel/compat/MarbleCompatibility.java | 9 - .../cricketcraft/chisel/init/ModBlocks.java | 13 +- 5 files changed, 76 insertions(+), 231 deletions(-) delete mode 100644 src/main/java/com/cricketcraft/chisel/ChiselLeftClick.java diff --git a/src/main/java/com/cricketcraft/chisel/Chisel.java b/src/main/java/com/cricketcraft/chisel/Chisel.java index 3fd2d11d..d5a723cf 100644 --- a/src/main/java/com/cricketcraft/chisel/Chisel.java +++ b/src/main/java/com/cricketcraft/chisel/Chisel.java @@ -3,6 +3,8 @@ package com.cricketcraft.chisel; import com.cricketcraft.chisel.block.BlockCarvable; import com.cricketcraft.chisel.block.tileentity.TileEntityAutoChisel; import com.cricketcraft.chisel.block.tileentity.TileEntityPresent; +import com.cricketcraft.chisel.carving.Carving; +import com.cricketcraft.chisel.carving.CarvingVariation; import com.cricketcraft.chisel.client.gui.GuiAutoChisel; import com.cricketcraft.chisel.client.gui.GuiChisel; import com.cricketcraft.chisel.client.gui.GuiPresent; @@ -18,6 +20,7 @@ import com.cricketcraft.chisel.inventory.ContainerPresent; import com.cricketcraft.chisel.inventory.InventoryChiselSelection; import com.cricketcraft.chisel.proxy.CommonProxy; import com.cricketcraft.chisel.utils.General; +import com.cricketcraft.chisel.utils.GeneralClient; import com.cricketcraft.chisel.world.GeneratorChisel; import cpw.mods.fml.client.event.ConfigChangedEvent; import cpw.mods.fml.common.FMLCommonHandler; @@ -38,10 +41,13 @@ import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; +import org.apache.commons.lang3.ArrayUtils; import java.io.File; import java.util.ArrayList; @@ -188,7 +194,6 @@ public class Chisel proxy.init(); MinecraftForge.EVENT_BUS.register(this); - MinecraftForge.EVENT_BUS.register(new ChiselLeftClick()); FMLCommonHandler.instance().bus().register(instance); FMLInterModComms.sendMessage("Waila", "register", "com.cricketcraft.chisel.Waila.register"); @@ -208,5 +213,52 @@ public class Chisel Configurations.refreshConfig(); } } + @SubscribeEvent + public void onLeftClickBlock(PlayerInteractEvent event) + { + ItemStack held = event.entityPlayer.getCurrentEquippedItem(); + int slot = event.entityPlayer.inventory.currentItem; + Carving carving = new Carving(); + if (event.action == PlayerInteractEvent.Action.LEFT_CLICK_BLOCK && held != null && held.getItem().equals(Configurations.featureEnabled("diamondChisel") ? ModItems.diamondChisel : ModItems.chisel)) + { + int x = event.x, y = event.y, z = event.z; + Block block = event.world.getBlock(x, y, z); + int metadata = event.world.getBlockMetadata(x, y, z); + CarvingVariation[] variations = carving.getVariations(block, metadata); + + if (variations != null) + { + ItemStack target = new ItemStack(block, metadata); + + if (target != null) + { + for (CarvingVariation v : variations) + { + if (v.block == Block.getBlockFromItem(target.getItem()) && v.meta == target.getItemDamage()) + { + event.world.setBlock(x, y, z, v.block); + } + } + } + else + { + for (int i = 0; i < variations.length; i++) + { + CarvingVariation v = variations[i]; + if (v.block == block && v.meta == metadata) + { + variations = ArrayUtils.remove(variations, i--); + } + } + + int index = event.world.rand.nextInt(variations.length); + CarvingVariation newVar = variations[index]; + event.world.setBlock(x, y, z, newVar.block); + GeneralClient.playChiselSound(event.world, x, y, z, MOD_ID + ":chisel.fallback"); + event.entityPlayer.inventory.currentItem = slot; + } + } + } + } } diff --git a/src/main/java/com/cricketcraft/chisel/ChiselLeftClick.java b/src/main/java/com/cricketcraft/chisel/ChiselLeftClick.java deleted file mode 100644 index 1c4840b9..00000000 --- a/src/main/java/com/cricketcraft/chisel/ChiselLeftClick.java +++ /dev/null @@ -1,186 +0,0 @@ -package com.cricketcraft.chisel; - -import com.cricketcraft.chisel.api.ChiselMode; -import com.cricketcraft.chisel.carving.CarvingVariation; -import com.cricketcraft.chisel.client.GeneralChiselClient; -import com.cricketcraft.chisel.init.ModBlocks; -import com.cricketcraft.chisel.init.ModItems; -import com.cricketcraft.chisel.item.ItemDiamondChisel; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import net.minecraft.block.Block; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.world.World; -import net.minecraftforge.common.util.ForgeDirection; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; -import net.minecraftforge.event.world.BlockEvent; - -import java.util.HashMap; -import java.util.Random; - -public class ChiselLeftClick { - HashMap chiselUseTime = new HashMap(); - HashMap chiselUseLocation = new HashMap(); - Random random = new Random(); - - public static void setBlock(World world, int x, int y, int z, ChiselMode chiselMode, Block previousBlock, int previousMetadata, Block newBlock, int newMetadata, ForgeDirection direction) { - if (chiselMode == ChiselMode.SINGLE) - { - world.setBlock(x, y, z, newBlock, newMetadata, 2); - } else if (chiselMode == ChiselMode.CIRCLETHREE) - { - int radius = 1; - for (int i = radius; i <= radius; i++) - for (int j = radius; j <= radius; j++) - { - //TODO facing stuff so this can be anywhere - if (world.getBlock(x + i, y, z + j) != null) - { - if (world.getBlock(x + i, y, z + j) == previousBlock && world.getBlockMetadata(x + i, y, z + j) == previousMetadata) { - int shiftedX = x; - int shiftedY = y; - int shiftedZ = z; - - world.setBlock(shiftedX, shiftedY, shiftedZ, newBlock, newMetadata, 2); - } - } - } - - } - - } - - @SubscribeEvent - public void onPlayerClick(PlayerInteractEvent event) { - if (event.action != PlayerInteractEvent.Action.LEFT_CLICK_BLOCK) return; - if (!Configurations.featureEnabled("diamondChisel")) return; - EntityPlayer player = event.entityPlayer; - ItemStack stack = player.getHeldItem(); - if (stack == null || stack.getItem() != ModItems.diamondChisel) return; - - World world = event.world; - int x = event.x; - int y = event.y; - int z = event.z; - - Block block = world.getBlock(x, y, z); - int blockMeta = world.getBlockMetadata(x, y, z); - - ItemStack chiselTarget = null; - - if (stack.stackTagCompound != null) { - chiselTarget = ItemStack.loadItemStackFromNBT(stack.stackTagCompound.getCompoundTag("chiselTarget")); - } - - boolean chiselHasBlockInside = true; - - if (chiselTarget == null) { - chiselHasBlockInside = false; - - Long useTime = chiselUseTime.get(player.getCommandSenderName()); - String loc = chiselUseLocation.get(player.getCommandSenderName()); - - if (useTime != null && chiselUseLocation != null && loc.equals(x + "|" + y + "|" + z)) { - long cooldown = 20; - long time = world.getWorldInfo().getWorldTotalTime(); - - if (time > useTime - cooldown && time < useTime + cooldown) return; //noReplace = true; - } - - CarvingVariation[] variations = ItemDiamondChisel.carving.getVariations(block, blockMeta); - if (variations == null || variations.length < 2) return; //noReplace = true; - else { - int index = 1; - //Find the index of the next block in the variation list - for (int i = 0; i < variations.length; ++i) { - CarvingVariation currVariation = variations[i]; - - //If the metadata matches, then we are interested in the block AFTER this one - if (currVariation.block.equals(block) && currVariation.meta == blockMeta) { - index = i + 1; - } - } - //If no index was found, something is wrong. Return. - if (index < 0) { - return; - } - //If the the current block is the last in the list, loop back to the first - else if (index >= variations.length) { - index = 0; - } - - CarvingVariation var = variations[index]; - chiselTarget = new ItemStack(var.block, 1, var.damage); - } - } - Item result = null; - int targetMeta = 0; - - Item target = chiselTarget.getItem(); - - targetMeta = chiselTarget.getItemDamage(); - - boolean match = ItemDiamondChisel.carving.isVariationOfSameClass(Block.getBlockFromItem(target), targetMeta, block, blockMeta); - result = target; - - /* special case: stone can be carved to cobble and bricks */ - if (Configurations.chiselStoneToCobbleBricks) { - if (!match && block.equals(Blocks.stone) && Block.getBlockFromItem(target).equals(ModBlocks.cobblestone)) - match = true; - if (!match && block.equals(Blocks.stone) && Block.getBlockFromItem(target).equals(ModBlocks.stoneBrick)) - match = true; - } - if (!match) - return; //noReplace = true; - - int updateValue = 1; - - if (!world.isRemote || chiselHasBlockInside) { - world.setBlock(x, y, z, Block.getBlockFromItem(result), targetMeta, updateValue); - world.markBlockForUpdate(x, y, z); - } - - switch (FMLCommonHandler.instance().getEffectiveSide()) { - case SERVER: - chiselUseTime.put(player.getCommandSenderName(), world.getWorldInfo().getWorldTotalTime()); - chiselUseLocation.put(player.getCommandSenderName(), x + "|" + y + "|" + z); - - try { - //TODO chisel left click thingy - // Packet packet = Chisel.packet.create(Packets.CHISELED).writeInt(x).writeInt(y).writeInt(z); - // Chisel.packet.sendToAllAround(packet, new TargetPoint(player.dimension, x, y, z, 30.0f)); - } catch (Exception e) { - e.printStackTrace(); - } - break; - - case CLIENT: - if (chiselHasBlockInside) { - String sound = ItemDiamondChisel.carving.getVariationSound(result, chiselTarget.getItemDamage()); - GeneralChiselClient.spawnChiselEffect(x, y, z, sound); - } - break; - - default: - break; - } - - stack.damageItem(1, player); - if (stack.stackSize == 0) { - player.inventory.mainInventory[player.inventory.currentItem] = chiselHasBlockInside ? chiselTarget : null; - } - } - - @SubscribeEvent - public void onBlockBreak(BlockEvent.BreakEvent event) { - if (!Configurations.featureEnabled("diamondChisel")) return; - EntityPlayer player = event.getPlayer(); - ItemStack stack = player.getHeldItem(); - if (stack == null || stack.getItem() != ModItems.diamondChisel) return; - - event.setCanceled(true); - } -} diff --git a/src/main/java/com/cricketcraft/chisel/block/BlockPresent.java b/src/main/java/com/cricketcraft/chisel/block/BlockPresent.java index 3b4f8b43..99745cad 100644 --- a/src/main/java/com/cricketcraft/chisel/block/BlockPresent.java +++ b/src/main/java/com/cricketcraft/chisel/block/BlockPresent.java @@ -4,7 +4,6 @@ import com.cricketcraft.chisel.api.ICarvable; import com.cricketcraft.chisel.block.tileentity.TileEntityPresent; import com.cricketcraft.chisel.carving.CarvableHelper; import com.cricketcraft.chisel.carving.CarvableVariation; -import com.cricketcraft.chisel.init.ModTabs; import com.cricketcraft.chisel.inventory.InventoryLargePresent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -26,7 +25,6 @@ import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; -import java.util.Calendar; import java.util.Iterator; import java.util.Random; @@ -34,57 +32,48 @@ public class BlockPresent extends BlockChest implements ICarvable { private final Random random = new Random(); private int type; public CarvableHelper carverHelper; - public boolean isChristmas; public BlockPresent(int type) { super(1); this.type = type; carverHelper = new CarvableHelper(); setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0875F, 0.9365F); - - Calendar calendar = Calendar.getInstance(); - //Unneeded? - if (calendar.get(2) + 1 == 12 || calendar.get(2) + 1 == 1) { - isChristmas = true; //The Christmas Season lasts for december and january - } else { - isChristmas = false; //TODO: THESE NEED TO EXIST - } } public String getKindOfChest(int type){ switch (type){ - case 0: //TODO: |----------these are fine-------------| |------these are missing-------| - return isChristmas? "textures/blocks/present/presentChest0" : "textures/blocks/present/chest0"; + case 0: + return "textures/blocks/present/presentChest0"; case 1: - return isChristmas? "textures/blocks/present/presentChest1" : "textures/blocks/present/chest1"; + return "textures/blocks/present/presentChest1"; case 2: - return isChristmas? "textures/blocks/present/presentChest2" : "textures/blocks/present/chest2"; + return "textures/blocks/present/presentChest2"; case 3: - return isChristmas? "textures/blocks/present/presentChest3" : "textures/blocks/present/chest3"; + return "textures/blocks/present/presentChest3"; case 4: - return isChristmas? "textures/blocks/present/presentChest4" : "textures/blocks/present/chest4"; + return "textures/blocks/present/presentChest4"; case 5: - return isChristmas? "textures/blocks/present/presentChest5" : "textures/blocks/present/chest5"; + return "textures/blocks/present/presentChest5"; case 6: - return isChristmas? "textures/blocks/present/presentChest6" : "textures/blocks/present/chest6"; + return "textures/blocks/present/presentChest6"; case 7: - return isChristmas? "textures/blocks/present/presentChest7" : "textures/blocks/present/chest7"; + return "textures/blocks/present/presentChest7"; case 8: - return isChristmas? "textures/blocks/present/presentChest8" : "textures/blocks/present/chest8"; + return "textures/blocks/present/presentChest8"; case 9: - return isChristmas? "textures/blocks/present/presentChest9" : "textures/blocks/present/chest9"; + return "textures/blocks/present/presentChest9"; case 10: - return isChristmas? "textures/blocks/present/presentChest10" : "textures/blocks/present/chest10"; + return "textures/blocks/present/presentChest10"; case 11: - return isChristmas? "textures/blocks/present/presentChest11" : "textures/blocks/present/chest11"; + return "textures/blocks/present/presentChest11"; case 12: - return isChristmas? "textures/blocks/present/presentChest12" : "textures/blocks/present/chest12"; + return "textures/blocks/present/presentChest12"; case 13: - return isChristmas? "textures/blocks/present/presentChest13" : "textures/blocks/present/chest13"; + return "textures/blocks/present/presentChest13"; case 14: - return isChristmas? "textures/blocks/present/presentChest14" : "textures/blocks/present/chest14"; + return "textures/blocks/present/presentChest14"; case 15: - return isChristmas? "textures/blocks/present/presentChest15" : "textures/blocks/present/chest15"; + return "textures/blocks/present/presentChest15"; default: return null; } diff --git a/src/main/java/com/cricketcraft/chisel/compat/MarbleCompatibility.java b/src/main/java/com/cricketcraft/chisel/compat/MarbleCompatibility.java index eefd767f..38989be5 100644 --- a/src/main/java/com/cricketcraft/chisel/compat/MarbleCompatibility.java +++ b/src/main/java/com/cricketcraft/chisel/compat/MarbleCompatibility.java @@ -80,7 +80,6 @@ public class MarbleCompatibility ClassBlockCompat.this.doAction(block); } }; - block.setHarvestLevel("chisel", 0, 0); } }; } @@ -104,7 +103,6 @@ public class MarbleCompatibility void action() { Carving.chisel.addVariation("marble", block, 0, 99); - block.setHarvestLevel("chisel", 0, 0); } }; @@ -116,7 +114,6 @@ public class MarbleCompatibility Block[] blocks = (Block[]) cl.getField("blockLimestones").get(null); Carving.chisel.addVariation("limestone", blocks[0], 3, 99); - blocks[0].setHarvestLevel("chisel", 0, 3); } }; @@ -126,7 +123,6 @@ public class MarbleCompatibility void action() { Carving.chisel.addVariation("marble", block, 0, 99); - block.setHarvestLevel("chisel", 0, 0); } }; @@ -136,7 +132,6 @@ public class MarbleCompatibility void action() { Carving.chisel.addVariation("marble", block, 0, 99); - block.setHarvestLevel("chisel", 0, 0); } }; @@ -146,7 +141,6 @@ public class MarbleCompatibility void action() { Carving.chisel.addVariation("limestone", block, 0, 99); - block.setHarvestLevel("chisel", 0, 0); } }; new ClassBlockCompat("emasher.core.EmasherCore", "limestone") @@ -155,7 +149,6 @@ public class MarbleCompatibility void action() { Carving.chisel.addVariation("limestone", block, 0, 99); - block.setHarvestLevel("chisel", 0, 0); } }; @@ -167,10 +160,8 @@ public class MarbleCompatibility Block[] blocks = (Block[]) cl.getField("blocks").get(null); Carving.chisel.addVariation("marble", blocks[0], 2, 99); - blocks[0].setHarvestLevel("chisel", 0, 2); Carving.chisel.addVariation("limestone", blocks[0], 3, 99); - blocks[0].setHarvestLevel("chisel", 0, 3); } }; } diff --git a/src/main/java/com/cricketcraft/chisel/init/ModBlocks.java b/src/main/java/com/cricketcraft/chisel/init/ModBlocks.java index b51f6d99..de654bbf 100644 --- a/src/main/java/com/cricketcraft/chisel/init/ModBlocks.java +++ b/src/main/java/com/cricketcraft/chisel/init/ModBlocks.java @@ -104,15 +104,14 @@ public class ModBlocks { public static BlockCarvablePane[] stainedGlassPane = new BlockCarvablePane[8]; //1.8 or at least so you think :P - public static BlockCarvable granite, polishedGranite; - public static BlockCarvable diorite, polishedDiorite; - public static BlockCarvable andesite, polishedAndesite; + public static BlockCarvable granite; + public static BlockCarvable diorite; + public static BlockCarvable andesite; + + private static boolean areChestsReady = false; public static void load() { - Blocks.stone.setHarvestLevel("chisel", 0); - Blocks.sand.setHarvestLevel("chisel", 0); - if(Configurations.featureEnabled("autoChisel")){ autoChisel = new BlockAutoChisel().setBlockTextureName("Chisel:autoChisel").setCreativeTab(ModTabs.tabChisel).setBlockName("autoChisel").setBlockTextureName(Chisel.MOD_ID + ":factory/circuit"); GameRegistry.registerBlock(autoChisel, "autoChisel"); @@ -1443,7 +1442,7 @@ public class ModBlocks { Carving.chisel.registerOre("leaves", "leaves"); } - if (Configurations.featureEnabled("chest")) { + if (areChestsReady) { for(int x = 0; x < 16; x++){ present[x] = (BlockPresent) new BlockPresent(x).setCreativeTab(ModTabs.tabOtherChiselBlocks).setHardness(2.0F).setResistance(10.0F).setBlockName("present"); GameRegistry.registerBlock(present[x], "chest" + x);