diff --git a/src/main/java/taintedmagic/common/items/ItemFlyteCharm.java b/src/main/java/taintedmagic/common/items/ItemFlyteCharm.java index 4236ca0..9ddb3d3 100644 --- a/src/main/java/taintedmagic/common/items/ItemFlyteCharm.java +++ b/src/main/java/taintedmagic/common/items/ItemFlyteCharm.java @@ -1,9 +1,15 @@ package taintedmagic.common.items; +import java.util.HashMap; +import java.util.UUID; + +import org.lwjgl.input.Keyboard; import org.lwjgl.opengl.GL11; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent; +import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.client.Minecraft; @@ -19,6 +25,8 @@ import net.minecraftforge.event.entity.living.LivingEvent; import taintedmagic.api.IRenderInventoryItem; import taintedmagic.common.TaintedMagic; import taintedmagic.common.helper.TaintedMagicHelper; +import taintedmagic.common.network.PacketHandler; +import taintedmagic.common.network.PacketUpdateJumpKey; import thaumcraft.api.IWarpingGear; import thaumcraft.api.aspects.Aspect; import thaumcraft.api.aspects.AspectList; @@ -26,11 +34,17 @@ import thaumcraft.client.lib.UtilsFX; public class ItemFlyteCharm extends Item implements IWarpingGear, IRenderInventoryItem { - // Vis used per tick while flying - static final AspectList COST = new AspectList().add(Aspect.AIR, 15); + // Vis used per tick while flying / boosting / gliding + private static final AspectList COST_FLIGHT = new AspectList().add(Aspect.AIR, 15); + private static final AspectList COST_BOOST = new AspectList().add(Aspect.AIR, 5).add(Aspect.FIRE, 10); + private static final AspectList COST_GLIDE = new AspectList().add(Aspect.AIR, 5); + // Magic circle texture private static final ResourceLocation MAGIC_CIRCLE = new ResourceLocation("taintedmagic:textures/misc/circle.png"); + // Stores whether the player has their jump key down (for boost function) + public static HashMap jumpKeyState = new HashMap(); + public ItemFlyteCharm () { this.setCreativeTab(TaintedMagic.tabTM); @@ -54,57 +68,97 @@ public class ItemFlyteCharm extends Item implements IWarpingGear, IRenderInvento if (event.entityLiving instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) event.entityLiving; - boolean isFlying = player.capabilities.isFlying; if (shouldPlayerHaveFlight(player)) { - player.capabilities.allowFlying = true; + boolean isFlying = player.capabilities.isFlying; - if (isFlying && !player.capabilities.isCreativeMode) - TaintedMagicHelper.consumeVisFromInventory(player, COST, true); - - // Speed boost - if (player.moveForward > 0.0F) + if (TaintedMagicHelper.consumeVisFromInventory(player, COST_FLIGHT, isFlying)) + player.capabilities.allowFlying = true; // Allow flight + else if (!player.capabilities.isCreativeMode) { - float mul = 0.05F; - player.moveFlying(0.0F, 1.0F, mul); - player.jumpMovementFactor = 0.00002F; + player.capabilities.allowFlying = false; + player.capabilities.isFlying = false; } if (!isFlying) { // Glide - if (player.isSneaking() && !player.onGround && player.fallDistance > 0.5F) + if (player.isSneaking() && !player.onGround && player.fallDistance > 0.5F + && TaintedMagicHelper.consumeVisFromInventory(player, COST_GLIDE, true)) { double speed = 0.1D; player.motionY = -speed; - double x = Math.cos(Math.toRadians(player.rotationYawHead + 90)) * speed; - double z = Math.sin(Math.toRadians(player.rotationYawHead + 90)) * speed; - player.motionX += x; - player.motionZ += z; + player.motionX += Math.cos(Math.toRadians(player.rotationYawHead + 90)) * speed; + player.motionZ += Math.sin(Math.toRadians(player.rotationYawHead + 90)) * speed; } + + // Boost + if (!player.isSneaking() && jumpKeyState.containsKey(player.getUniqueID()) + && jumpKeyState.get(player.getUniqueID()) + && TaintedMagicHelper.consumeVisFromInventory(player, COST_BOOST, player.motionY > 0.2)) + player.motionY = Math.min(player.motionY + 0.2D, 0.6D); + } - } - else - { - if (!player.capabilities.isCreativeMode) + // Speed boost + if (player.moveForward > 0.0F) { - player.capabilities.allowFlying = false; - player.capabilities.isFlying = false; + float mul = 0.02F; + player.moveFlying(0.0F, 1.0F, mul); } } + else if (!player.capabilities.isCreativeMode) + { + player.capabilities.allowFlying = false; + player.capabilities.isFlying = false; + } + } + } + + @SubscribeEvent + public void onPlayerLoggedOut (PlayerLoggedInEvent event) + { + // Remove the player from the HashMap + if (jumpKeyState.containsKey(event.player.getUniqueID())) jumpKeyState.remove(event.player.getUniqueID()); + } + + /** + * Updates the player's jump key status & syncs it to the server + * + * @param event + */ + @SubscribeEvent + @SideOnly (Side.CLIENT) + public void playerTick (ClientTickEvent event) + { + EntityPlayer player = TaintedMagic.proxy.getClientPlayer(); + if (player != null) + { + UUID uuid = player.getUniqueID(); + if (shouldPlayerHaveFlight(player)) + { + boolean state = Minecraft.getMinecraft().gameSettings.keyBindJump.getIsKeyPressed(); + if (jumpKeyState.containsKey(uuid) && jumpKeyState.get(uuid) != state) + { + jumpKeyState.replace(uuid, state); + PacketHandler.INSTANCE.sendToServer(new PacketUpdateJumpKey(uuid, state)); + } + else jumpKeyState.put(uuid, state); + } + else jumpKeyState.remove(uuid); } } /** * Determines if the player should be able to fly. + * + * @param player */ private boolean shouldPlayerHaveFlight (EntityPlayer player) { for (int i = 0; i < player.inventory.getSizeInventory(); i++) if (player.inventory.getStackInSlot(i) != null - && player.inventory.getStackInSlot(i).getItem() instanceof ItemFlyteCharm - && TaintedMagicHelper.consumeVisFromInventory(player, COST, false)) + && player.inventory.getStackInSlot(i).getItem() instanceof ItemFlyteCharm) return true; return false; } diff --git a/src/main/java/taintedmagic/common/network/PacketHandler.java b/src/main/java/taintedmagic/common/network/PacketHandler.java index d982170..eb3d85e 100644 --- a/src/main/java/taintedmagic/common/network/PacketHandler.java +++ b/src/main/java/taintedmagic/common/network/PacketHandler.java @@ -12,5 +12,6 @@ public class PacketHandler public static void initPackets () { INSTANCE.registerMessage(PacketKatanaAttack.class, PacketKatanaAttack.class, 0, Side.SERVER); + INSTANCE.registerMessage(PacketUpdateJumpKey.class, PacketUpdateJumpKey.class, 1, Side.SERVER); } } diff --git a/src/main/java/taintedmagic/common/network/PacketUpdateJumpKey.java b/src/main/java/taintedmagic/common/network/PacketUpdateJumpKey.java new file mode 100644 index 0000000..2833d09 --- /dev/null +++ b/src/main/java/taintedmagic/common/network/PacketUpdateJumpKey.java @@ -0,0 +1,49 @@ +package taintedmagic.common.network; + +import java.util.UUID; + +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 taintedmagic.common.items.ItemFlyteCharm; + +public class PacketUpdateJumpKey implements IMessage, IMessageHandler +{ + private long leastSigBits, mostSigBits; + private boolean state; + + public PacketUpdateJumpKey () + { + } + + public PacketUpdateJumpKey (UUID uuid, boolean state) + { + this.leastSigBits = uuid.getLeastSignificantBits(); + this.mostSigBits = uuid.getMostSignificantBits(); + this.state = state; + } + + @Override + public IMessage onMessage (PacketUpdateJumpKey message, MessageContext ctx) + { + ItemFlyteCharm.jumpKeyState.replace(new UUID(message.leastSigBits, message.mostSigBits), message.state); + return null; + } + + @Override + public void fromBytes (ByteBuf buf) + { + this.leastSigBits = buf.readLong(); + this.mostSigBits = buf.readLong(); + this.state = buf.readBoolean(); + } + + @Override + public void toBytes (ByteBuf buf) + { + buf.writeLong(leastSigBits); + buf.writeLong(mostSigBits); + buf.writeBoolean(state); + } +}