From 828a1e4f4a574170fc64723965060a7fb4b2b2f0 Mon Sep 17 00:00:00 2001 From: James McKay Date: Fri, 16 Oct 2015 13:19:02 +1100 Subject: [PATCH] Implement basic mode switching. --- .../bbw}/BetterBuildersWandsMod.java | 32 +++++++- .../java/portablejim/bbw/basics/EnumLock.java | 2 +- .../portablejim/bbw/core/BlockEvents.java | 34 +++++---- .../portablejim/bbw/core/WandUtility.java | 5 +- .../java/portablejim/bbw/core/WandWorker.java | 1 + .../bbw/core/client/KeyEvents.java | 38 ++++++++++ .../portablejim/bbw/core/items/IWandItem.java | 9 +++ .../bbw/core/items/ItemBasicWand.java | 12 +-- .../bbw/core/items/ItemUnbreakableWand.java | 47 ++++++++++++ .../bbw/network/GenericHandler.java | 19 +++++ .../bbw/network/PacketWandActivate.java | 75 +++++++++++++++++++ .../portablejim/bbw/proxy/ClientProxy.java | 16 ++++ .../portablejim/bbw/proxy/CommonProxy.java | 11 +++ .../java/portablejim/bbw/proxy/IProxy.java | 8 ++ .../portablejim/bbw/proxy/ServerProxy.java | 7 ++ 15 files changed, 286 insertions(+), 30 deletions(-) rename src/main/java/{ => portablejim/bbw}/BetterBuildersWandsMod.java (50%) create mode 100644 src/main/java/portablejim/bbw/core/client/KeyEvents.java create mode 100644 src/main/java/portablejim/bbw/network/GenericHandler.java create mode 100644 src/main/java/portablejim/bbw/network/PacketWandActivate.java create mode 100644 src/main/java/portablejim/bbw/proxy/ClientProxy.java create mode 100644 src/main/java/portablejim/bbw/proxy/CommonProxy.java create mode 100644 src/main/java/portablejim/bbw/proxy/IProxy.java create mode 100644 src/main/java/portablejim/bbw/proxy/ServerProxy.java diff --git a/src/main/java/BetterBuildersWandsMod.java b/src/main/java/portablejim/bbw/BetterBuildersWandsMod.java similarity index 50% rename from src/main/java/BetterBuildersWandsMod.java rename to src/main/java/portablejim/bbw/BetterBuildersWandsMod.java index c13c737..e66a315 100644 --- a/src/main/java/BetterBuildersWandsMod.java +++ b/src/main/java/portablejim/bbw/BetterBuildersWandsMod.java @@ -1,14 +1,24 @@ -import cpw.mods.fml.client.FMLClientHandler; -import cpw.mods.fml.client.registry.ClientRegistry; +package portablejim.bbw; + import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; +import cpw.mods.fml.common.SidedProxy; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.gameevent.InputEvent; +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.common.registry.GameRegistry; +import cpw.mods.fml.relauncher.Side; import net.minecraftforge.common.MinecraftForge; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.simple.SimpleLogger; +import org.apache.logging.log4j.util.PropertiesUtil; import portablejim.bbw.core.BlockEvents; import portablejim.bbw.core.items.ItemUnbreakableWand; +import portablejim.bbw.network.PacketWandActivate; +import portablejim.bbw.proxy.IProxy; /** * Author: Portablejim @@ -18,18 +28,34 @@ public class BetterBuildersWandsMod { public static final String MODID = "betterbuilderswands"; public static final String VERSION = "0.1"; + @Mod.Instance + public static BetterBuildersWandsMod instance; + + @SidedProxy(modId = MODID, clientSide = "portablejim.bbw.proxy.ClientProxy", serverSide = "portablejim.bbw.proxy.ServerProxy") + public static IProxy proxy; + + public static Logger logger = new SimpleLogger("BetterBuildersWand", Level.ALL, true, false, true, false, "YYYY-MM-DD", null, PropertiesUtil.getProperties(), null); + public static ItemUnbreakableWand itemUnbreakableWand; + public SimpleNetworkWrapper networkWrapper; + @EventHandler public void preInit(FMLPreInitializationEvent event) { - itemUnbreakableWand = new ItemUnbreakableWand(); + logger = event.getModLog(); + itemUnbreakableWand = new ItemUnbreakableWand(); GameRegistry.registerItem(itemUnbreakableWand, "unbreakableWand"); + + networkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel("bbwands"); + networkWrapper.registerMessage(PacketWandActivate.Handler.class, PacketWandActivate.class, 0, Side.SERVER); + } @EventHandler public void init(FMLInitializationEvent event) { MinecraftForge.EVENT_BUS.register(new BlockEvents()); + proxy.RegisterEvents(); } @EventHandler diff --git a/src/main/java/portablejim/bbw/basics/EnumLock.java b/src/main/java/portablejim/bbw/basics/EnumLock.java index 4ff0f37..72580fe 100644 --- a/src/main/java/portablejim/bbw/basics/EnumLock.java +++ b/src/main/java/portablejim/bbw/basics/EnumLock.java @@ -26,6 +26,6 @@ public enum EnumLock { EnumLock locks[] = { NORTHSOUTH, VERTICAL, VERTICALEASTWEST, EASTWEST, HORIZONTAL, VERTICALNORTHSOUTH, NOLOCK }; int safeMask = inputMask & 7; - return locks[safeMask]; + return locks[safeMask - 1]; } } diff --git a/src/main/java/portablejim/bbw/core/BlockEvents.java b/src/main/java/portablejim/bbw/core/BlockEvents.java index 32ce32e..371964a 100644 --- a/src/main/java/portablejim/bbw/core/BlockEvents.java +++ b/src/main/java/portablejim/bbw/core/BlockEvents.java @@ -11,6 +11,7 @@ import net.minecraft.util.MovingObjectPosition; import net.minecraftforge.client.event.DrawBlockHighlightEvent; import net.minecraftforge.common.util.ForgeDirection; import org.lwjgl.opengl.GL11; +import portablejim.bbw.IWand; import portablejim.bbw.basics.EnumLock; import portablejim.bbw.basics.Point3d; import portablejim.bbw.core.items.IWandItem; @@ -36,26 +37,29 @@ public class BlockEvents { playerShim = new CreativePlayerShim(event.player); } IWorldShim worldShim = new BasicWorldShim(event.player.getEntityWorld()); - UnbreakingWand unbreakingWand = new UnbreakingWand(event.currentItem); + if(event.currentItem.getItem() instanceof IWandItem) { + IWandItem wandItem = (IWandItem) event.currentItem.getItem(); + IWand wand = wandItem.getWand(event.currentItem); - WandWorker worker = new WandWorker(unbreakingWand, playerShim, worldShim); + WandWorker worker = new WandWorker(wand, playerShim, worldShim); - Point3d clickedPos = new Point3d(event.target.blockX, event.target.blockY, event.target.blockZ); + Point3d clickedPos = new Point3d(event.target.blockX, event.target.blockY, event.target.blockZ); - ItemStack targetItemstack = worker.getEquivalentItemStack(clickedPos); - int numBlocks = Math.min(unbreakingWand.getMaxBlocks(), playerShim.countItems(targetItemstack)); + ItemStack targetItemstack = worker.getEquivalentItemStack(clickedPos); + int numBlocks = Math.min(wand.getMaxBlocks(), playerShim.countItems(targetItemstack)); - LinkedList blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(event.target.sideHit), numBlocks, EnumLock.HORIZONTAL, EnumLock.HORIZONTAL); - if(blocks.size() > 0) { - GL11.glDisable(GL11.GL_TEXTURE_2D); - GL11.glDepthMask(false); - GL11.glLineWidth(2.5F); - for(Point3d block : blocks) { - AxisAlignedBB boundingBox = AxisAlignedBB.getBoundingBox(block.x, block.y, block.z, block.x + 1, block.y + 1, block.z + 1).contract(0.005, 0.005, 0.005); - RenderGlobal.drawOutlinedBoundingBox(boundingBox.getOffsetBoundingBox(-event.player.posX, -event.player.posY, -event.player.posZ), 0xC0C0C0); + LinkedList blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(event.target.sideHit), numBlocks, wand.getMode(), wandItem.getFaceLock(event.currentItem)); + if (blocks.size() > 0) { + GL11.glDisable(GL11.GL_TEXTURE_2D); + GL11.glDepthMask(false); + GL11.glLineWidth(2.5F); + for (Point3d block : blocks) { + AxisAlignedBB boundingBox = AxisAlignedBB.getBoundingBox(block.x, block.y, block.z, block.x + 1, block.y + 1, block.z + 1).contract(0.005, 0.005, 0.005); + RenderGlobal.drawOutlinedBoundingBox(boundingBox.getOffsetBoundingBox(-event.player.posX, -event.player.posY, -event.player.posZ), 0xC0C0C0); + } + GL11.glEnable(GL11.GL_TEXTURE_2D); + GL11.glDisable(GL11.GL_BLEND); } - GL11.glEnable(GL11.GL_TEXTURE_2D); - GL11.glDisable(GL11.GL_BLEND); } } //FMLLog.info("Happened!" + event.target.toString()); diff --git a/src/main/java/portablejim/bbw/core/WandUtility.java b/src/main/java/portablejim/bbw/core/WandUtility.java index a36aeea..62eaab7 100644 --- a/src/main/java/portablejim/bbw/core/WandUtility.java +++ b/src/main/java/portablejim/bbw/core/WandUtility.java @@ -6,6 +6,7 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.Constants; import portablejim.bbw.IWand; import portablejim.bbw.basics.EnumLock; +import portablejim.bbw.core.items.IWandItem; /** * Common functions across wands. @@ -22,10 +23,12 @@ public class WandUtility { } short shortMask = (short) (mode.mask & 7); bbwCompond.setShort("mask", shortMask); + tagCompound.setTag("bbw", bbwCompond); + item.setTagCompound(tagCompound); } public static EnumLock getMode(ItemStack item) { - if(item != null && item.getItem() != null && item.getItem() instanceof IWand) { + if(item != null && item.getItem() != null && item.getItem() instanceof IWandItem) { NBTTagCompound itemBaseNBT = item.getTagCompound(); if(itemBaseNBT != null && itemBaseNBT.hasKey("bbw", Constants.NBT.TAG_COMPOUND)) { NBTTagCompound itemNBT = itemBaseNBT.getCompoundTag("bbw"); diff --git a/src/main/java/portablejim/bbw/core/WandWorker.java b/src/main/java/portablejim/bbw/core/WandWorker.java index f1808af..7759b44 100644 --- a/src/main/java/portablejim/bbw/core/WandWorker.java +++ b/src/main/java/portablejim/bbw/core/WandWorker.java @@ -8,6 +8,7 @@ import net.minecraftforge.common.util.ForgeDirection; import portablejim.bbw.IWand; import portablejim.bbw.basics.EnumLock; import portablejim.bbw.basics.Point3d; +import portablejim.bbw.core.items.IWandItem; import portablejim.bbw.shims.IPlayerShim; import portablejim.bbw.shims.IWorldShim; diff --git a/src/main/java/portablejim/bbw/core/client/KeyEvents.java b/src/main/java/portablejim/bbw/core/client/KeyEvents.java new file mode 100644 index 0000000..bea058c --- /dev/null +++ b/src/main/java/portablejim/bbw/core/client/KeyEvents.java @@ -0,0 +1,38 @@ +package portablejim.bbw.core.client; + +import cpw.mods.fml.client.registry.ClientRegistry; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.InputEvent; +import net.minecraft.client.settings.KeyBinding; +import org.lwjgl.input.Keyboard; +import portablejim.bbw.BetterBuildersWandsMod; +import portablejim.bbw.network.PacketWandActivate; + +/** + * Created by james on 16/10/15. + */ +public class KeyEvents { + public KeyBinding keyBinding = new KeyBinding("bbw.key.mode", Keyboard.KEY_M, "bbw.key.category"); + + private boolean isPressed; + + public KeyEvents() { + ClientRegistry.registerKeyBinding(keyBinding); + FMLCommonHandler.instance().bus().register(this); + isPressed = false; + } + + @SubscribeEvent + public void KeyEvent(InputEvent event) { + boolean currentIsPressed = keyBinding.getIsKeyPressed(); + if(currentIsPressed != isPressed) { + isPressed = currentIsPressed; + PacketWandActivate packet = new PacketWandActivate(currentIsPressed); + BetterBuildersWandsMod.instance.networkWrapper.sendToServer(packet); + } + + } + + +} diff --git a/src/main/java/portablejim/bbw/core/items/IWandItem.java b/src/main/java/portablejim/bbw/core/items/IWandItem.java index 5e2ae4a..cd282a4 100644 --- a/src/main/java/portablejim/bbw/core/items/IWandItem.java +++ b/src/main/java/portablejim/bbw/core/items/IWandItem.java @@ -1,7 +1,16 @@ package portablejim.bbw.core.items; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import portablejim.bbw.IWand; +import portablejim.bbw.basics.EnumLock; + /** * Identifies classes that should be treated as a wand. */ public interface IWandItem { + void nextMode(ItemStack itemStack, EntityPlayer player); + IWand getWand(ItemStack itemStack); + EnumLock getLock(ItemStack itemStack); + EnumLock getFaceLock(ItemStack itemStack); } diff --git a/src/main/java/portablejim/bbw/core/items/ItemBasicWand.java b/src/main/java/portablejim/bbw/core/items/ItemBasicWand.java index 8925a79..1be7242 100644 --- a/src/main/java/portablejim/bbw/core/items/ItemBasicWand.java +++ b/src/main/java/portablejim/bbw/core/items/ItemBasicWand.java @@ -22,7 +22,7 @@ import java.util.LinkedList; /** * Class to cover common functions between wands. */ -public class ItemBasicWand extends Item implements IWandItem{ +public abstract class ItemBasicWand extends Item implements IWandItem{ public ItemBasicWand() { super(); this.setCreativeTab(CreativeTabs.tabTools); @@ -44,18 +44,10 @@ public class ItemBasicWand extends Item implements IWandItem{ int numBlocks = Math.min(unbreakingWand.getMaxBlocks(), playerShim.countItems(targetItemstack)); FMLLog.info("Max blocks: %d (%d|%d", numBlocks, unbreakingWand.getMaxBlocks(), playerShim.countItems(targetItemstack)); - LinkedList blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(side), numBlocks, getLock(), getFaceLock()); + LinkedList blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(side), numBlocks, getLock(itemstack), getFaceLock(itemstack)); worker.placeBlocks(blocks, clickedPos); return true; } - - protected EnumLock getLock() { - return EnumLock.HORIZONTAL; - } - - protected EnumLock getFaceLock() { - return EnumLock.NOLOCK; - } } diff --git a/src/main/java/portablejim/bbw/core/items/ItemUnbreakableWand.java b/src/main/java/portablejim/bbw/core/items/ItemUnbreakableWand.java index 4854101..e1852bb 100644 --- a/src/main/java/portablejim/bbw/core/items/ItemUnbreakableWand.java +++ b/src/main/java/portablejim/bbw/core/items/ItemUnbreakableWand.java @@ -1,5 +1,13 @@ package portablejim.bbw.core.items; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import portablejim.bbw.IWand; +import portablejim.bbw.basics.EnumLock; +import portablejim.bbw.core.WandUtility; +import portablejim.bbw.core.WandWorker; +import portablejim.bbw.core.wands.UnbreakingWand; + /** * The actual item: Simple wand with no durability. Similar to current Builder's Wand. */ @@ -9,4 +17,43 @@ public class ItemUnbreakableWand extends ItemBasicWand{ this.setUnlocalizedName("betterbuilderswands:unbreakableWand"); this.setTextureName("betterbuilderswands:wandUnbreakable"); } + + public EnumLock getLock(ItemStack itemStack) { + IWand wand = new UnbreakingWand(itemStack); + return wand.getMode(); + } + + public EnumLock getFaceLock(ItemStack itemStack) { + return EnumLock.NOLOCK; + } + + @Override + public void nextMode(ItemStack itemStack, EntityPlayer player) { + IWand wand = new UnbreakingWand(itemStack); + switch(wand.getMode()) { + + case NORTHSOUTH: + break; + case VERTICAL: + WandUtility.setMode(itemStack, EnumLock.NOLOCK); + break; + case VERTICALEASTWEST: + break; + case EASTWEST: + break; + case HORIZONTAL: + WandUtility.setMode(itemStack, EnumLock.VERTICAL); + break; + case VERTICALNORTHSOUTH: + break; + case NOLOCK: + WandUtility.setMode(itemStack, EnumLock.HORIZONTAL); + break; + } + } + + @Override + public IWand getWand(ItemStack itemStack) { + return new UnbreakingWand(itemStack); + } } diff --git a/src/main/java/portablejim/bbw/network/GenericHandler.java b/src/main/java/portablejim/bbw/network/GenericHandler.java new file mode 100644 index 0000000..d5b5b12 --- /dev/null +++ b/src/main/java/portablejim/bbw/network/GenericHandler.java @@ -0,0 +1,19 @@ +package portablejim.bbw.network; + +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; + +/** + * Handler to redirect message processing. + * Used to easily unify 1.7 and 1.8 code in a thread-safe way. + */ +public abstract class GenericHandler implements IMessageHandler { + @Override + public IMessage onMessage(REQ message, MessageContext ctx) { + processMessage(message, ctx); + return null; + } + + public abstract void processMessage(REQ message, MessageContext context); +} diff --git a/src/main/java/portablejim/bbw/network/PacketWandActivate.java b/src/main/java/portablejim/bbw/network/PacketWandActivate.java new file mode 100644 index 0000000..0779201 --- /dev/null +++ b/src/main/java/portablejim/bbw/network/PacketWandActivate.java @@ -0,0 +1,75 @@ +/* This file is part of VeinMiner. + * + * VeinMiner is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * VeinMiner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with VeinMiner. + * If not, see . + */ + +package portablejim.bbw.network; + +import cpw.mods.fml.common.network.simpleimpl.IMessage; +import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; +import cpw.mods.fml.common.network.simpleimpl.MessageContext; +import io.netty.buffer.ByteBuf; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ChatComponentText; +import portablejim.bbw.BetterBuildersWandsMod; +import portablejim.bbw.core.WandUtility; +import portablejim.bbw.core.items.IWandItem; + +import java.util.UUID; + +/** + * Packet the client sends to the server to tell it to activate or deactivate + * for the player that sent it. + */ + +public class PacketWandActivate implements IMessage { + public boolean keyActive; + + @SuppressWarnings("UnusedDeclaration") + public PacketWandActivate() {} + + public PacketWandActivate(boolean keyActive) { + this.keyActive = keyActive; + } + + @Override + public void toBytes(ByteBuf buffer) { + buffer.writeBoolean(keyActive); + } + + @Override + public void fromBytes(ByteBuf buffer) { + keyActive = buffer.readBoolean(); + } + + public static class Handler extends GenericHandler { + @Override + public void processMessage(PacketWandActivate packetWandActivate, MessageContext context) { + EntityPlayerMP player = context.getServerHandler().playerEntity; + UUID playerName = player.getUniqueID(); + + + if(packetWandActivate.keyActive && player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() != null + && player.getCurrentEquippedItem().getItem() instanceof IWandItem) { + ItemStack wandItemstack = player.getCurrentEquippedItem(); + IWandItem wandItem = (IWandItem) wandItemstack.getItem(); + wandItem.nextMode(wandItemstack, player); + player.addChatMessage(new ChatComponentText("Wand mode switch! " + String.format("%s", WandUtility.getMode(wandItemstack).toString()))); + } + } + } +} diff --git a/src/main/java/portablejim/bbw/proxy/ClientProxy.java b/src/main/java/portablejim/bbw/proxy/ClientProxy.java new file mode 100644 index 0000000..e868cc0 --- /dev/null +++ b/src/main/java/portablejim/bbw/proxy/ClientProxy.java @@ -0,0 +1,16 @@ +package portablejim.bbw.proxy; + +import portablejim.bbw.core.client.KeyEvents; + +/** + * Created by james on 16/10/15. + */ +public class ClientProxy extends CommonProxy { + private KeyEvents keyevents; + + @Override + public void RegisterEvents() { + keyevents = new KeyEvents(); + + } +} diff --git a/src/main/java/portablejim/bbw/proxy/CommonProxy.java b/src/main/java/portablejim/bbw/proxy/CommonProxy.java new file mode 100644 index 0000000..5c3ae52 --- /dev/null +++ b/src/main/java/portablejim/bbw/proxy/CommonProxy.java @@ -0,0 +1,11 @@ +package portablejim.bbw.proxy; + +/** + * Created by james on 16/10/15. + */ +public class CommonProxy implements IProxy { + @Override + public void RegisterEvents() { + + } +} diff --git a/src/main/java/portablejim/bbw/proxy/IProxy.java b/src/main/java/portablejim/bbw/proxy/IProxy.java new file mode 100644 index 0000000..b4e1c8c --- /dev/null +++ b/src/main/java/portablejim/bbw/proxy/IProxy.java @@ -0,0 +1,8 @@ +package portablejim.bbw.proxy; + +/** + * Created by james on 16/10/15. + */ +public interface IProxy { + void RegisterEvents(); +} diff --git a/src/main/java/portablejim/bbw/proxy/ServerProxy.java b/src/main/java/portablejim/bbw/proxy/ServerProxy.java new file mode 100644 index 0000000..caac6bf --- /dev/null +++ b/src/main/java/portablejim/bbw/proxy/ServerProxy.java @@ -0,0 +1,7 @@ +package portablejim.bbw.proxy; + +/** + * Created by james on 16/10/15. + */ +public class ServerProxy extends CommonProxy { +}