Implement basic mode switching.

This commit is contained in:
James McKay committed 2015-10-16 13:19:02 +11:00
1 parent 00140f9ab3
commit 828a1e4f4a
15 files changed
+286 -30

No files matched your search

@@ -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
@@ -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];
}
}
@@ -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<Point3d> 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<Point3d> 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());
@@ -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");
@@ -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;
@@ -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);
}
}
}
@@ -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);
}
@@ -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<Point3d> blocks = worker.getBlockPositionList(clickedPos, ForgeDirection.getOrientation(side), numBlocks, getLock(), getFaceLock());
LinkedList<Point3d> 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;
}
}
@@ -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);
}
}
@@ -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<REQ extends IMessage> implements IMessageHandler<REQ, IMessage> {
@Override
public IMessage onMessage(REQ message, MessageContext ctx) {
processMessage(message, ctx);
return null;
}
public abstract void processMessage(REQ message, MessageContext context);
}
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<PacketWandActivate> {
@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())));
}
}
}
}
@@ -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();
}
}
@@ -0,0 +1,11 @@
package portablejim.bbw.proxy;
/**
* Created by james on 16/10/15.
*/
public class CommonProxy implements IProxy {
@Override
public void RegisterEvents() {
}
}
@@ -0,0 +1,8 @@
package portablejim.bbw.proxy;
/**
* Created by james on 16/10/15.
*/
public interface IProxy {
void RegisterEvents();
}
@@ -0,0 +1,7 @@
package portablejim.bbw.proxy;
/**
* Created by james on 16/10/15.
*/
public class ServerProxy extends CommonProxy {
}