diff --git a/src/main/java/ellpeck/actuallyadditions/event/EntityLivingEvent.java b/src/main/java/ellpeck/actuallyadditions/event/EntityLivingEvent.java index ea6520261..f45ef598d 100644 --- a/src/main/java/ellpeck/actuallyadditions/event/EntityLivingEvent.java +++ b/src/main/java/ellpeck/actuallyadditions/event/EntityLivingEvent.java @@ -4,8 +4,10 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import ellpeck.actuallyadditions.config.values.ConfigBoolValues; import ellpeck.actuallyadditions.config.values.ConfigIntValues; import ellpeck.actuallyadditions.items.InitItems; +import ellpeck.actuallyadditions.items.ItemWingsOfTheBats; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.passive.EntityOcelot; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; @@ -15,6 +17,7 @@ public class EntityLivingEvent{ @SubscribeEvent public void livingUpdateEvent(LivingUpdateEvent event){ + //Ocelots dropping Hair Balls if(!event.entityLiving.worldObj.isRemote){ if(event.entityLiving instanceof EntityOcelot){ EntityOcelot theOcelot = (EntityOcelot)event.entityLiving; @@ -26,6 +29,40 @@ public class EntityLivingEvent{ } } } + + //Wings allowing Flight + if(event.entityLiving instanceof EntityPlayer){ + EntityPlayer player = (EntityPlayer)event.entityLiving; + boolean wingsEquipped = ItemWingsOfTheBats.hasWingItem(player); + + //If Player isn't (really) winged + if(!ItemWingsOfTheBats.isPlayerWinged(player)){ + if(wingsEquipped){ + //Make the Player actually winged + ItemWingsOfTheBats.addWingsToPlayer(player); + } + } + //If Player is (or should be) winged + else{ + if(wingsEquipped){ + //Allow the Player to fly when he has Wings equipped every tick + //It appears to be reset somewhere if you don't update it every tick + //but I haven't found the place where it gets reset which is slightly odd + player.capabilities.allowFlying = true; + } + else{ + //Make the Player not winged + ItemWingsOfTheBats.removeWingsFromPlayer(player); + //Reset Player's Values + if(!player.capabilities.isCreativeMode){ + player.capabilities.allowFlying = false; + player.capabilities.isFlying = false; + //Enables Fall Damage again (Automatically gets disabled for some reason) + player.capabilities.disableDamage = false; + } + } + } + } } } diff --git a/src/main/java/ellpeck/actuallyadditions/event/InitEvents.java b/src/main/java/ellpeck/actuallyadditions/event/InitEvents.java index cc68e634f..de9375294 100644 --- a/src/main/java/ellpeck/actuallyadditions/event/InitEvents.java +++ b/src/main/java/ellpeck/actuallyadditions/event/InitEvents.java @@ -16,6 +16,7 @@ public class InitEvents{ Util.registerEvent(new TooltipEvent()); Util.registerEvent(new EntityLivingEvent()); Util.registerEvent(new BucketFillEvent()); + Util.registerEvent(new LogoutEvent()); MinecraftForge.TERRAIN_GEN_BUS.register(new WorldDecorationEvent()); } diff --git a/src/main/java/ellpeck/actuallyadditions/event/LogoutEvent.java b/src/main/java/ellpeck/actuallyadditions/event/LogoutEvent.java new file mode 100644 index 000000000..694ebea10 --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/event/LogoutEvent.java @@ -0,0 +1,16 @@ +package ellpeck.actuallyadditions.event; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent; +import ellpeck.actuallyadditions.items.ItemWingsOfTheBats; + +public class LogoutEvent{ + + @SubscribeEvent + public void onLogOutEvent(PlayerEvent.PlayerLoggedOutEvent event){ + //Remove Player from Wings' Fly Permission List + ItemWingsOfTheBats.removeWingsFromPlayer(event.player, true); + ItemWingsOfTheBats.removeWingsFromPlayer(event.player, false); + } + +} diff --git a/src/main/java/ellpeck/actuallyadditions/items/InitItems.java b/src/main/java/ellpeck/actuallyadditions/items/InitItems.java index 4830e9642..c28607493 100644 --- a/src/main/java/ellpeck/actuallyadditions/items/InitItems.java +++ b/src/main/java/ellpeck/actuallyadditions/items/InitItems.java @@ -97,6 +97,8 @@ public class InitItems{ public static Item itemTeleStaff; + public static Item itemWingsOfTheBats; + public static void init(){ ModUtil.LOGGER.info("Initializing Items..."); @@ -115,6 +117,9 @@ public class InitItems{ itemTeleStaff = new ItemTeleStaff(); ItemUtil.register(itemTeleStaff); + itemWingsOfTheBats = new ItemWingsOfTheBats(); + ItemUtil.register(itemWingsOfTheBats); + itemDrill = new ItemDrill(); ItemUtil.register(itemDrill); diff --git a/src/main/java/ellpeck/actuallyadditions/items/ItemWingsOfTheBats.java b/src/main/java/ellpeck/actuallyadditions/items/ItemWingsOfTheBats.java new file mode 100644 index 000000000..c19bc6910 --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/items/ItemWingsOfTheBats.java @@ -0,0 +1,111 @@ +package ellpeck.actuallyadditions.items; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import ellpeck.actuallyadditions.util.INameableItem; +import ellpeck.actuallyadditions.util.ItemUtil; +import ellpeck.actuallyadditions.util.ModUtil; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.EnumRarity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.IIcon; + +import java.util.ArrayList; +import java.util.List; + +public class ItemWingsOfTheBats extends Item implements INameableItem{ + + /** + * A List containing all of the Players that can currently fly + * Used so that Flight from other Mods' Items doesn't get broken when + * these Wings aren't worn + * + * Saves Remote Players specially to make Damage Display not bug + * when taking off the Wings + * (Because Client and Server LivingUpdateEvent get called kind of irregularly + * and not both at the same time (That's at least what I worked out when testing :D)) + */ + public static ArrayList wingedPlayers = new ArrayList<>(); + + public ItemWingsOfTheBats(){ + this.setMaxStackSize(1); + } + + /** + * Checks if the Player is winged + * @param player The Player + * @return Winged? + */ + public static boolean isPlayerWinged(EntityPlayer player){ + return wingedPlayers.contains(player.getUniqueID()+(player.worldObj.isRemote ? "-Remote" : "")); + } + + /** + * Removes the Player from the List of Players that have Wings + * @param player The Player + * @param worldRemote If the World the Player is in is remote + */ + public static void removeWingsFromPlayer(EntityPlayer player, boolean worldRemote){ + wingedPlayers.remove(player.getUniqueID()+(worldRemote ? "-Remote" : "")); + } + + /** + * Same as above, but Remote Checking is done automatically + */ + public static void removeWingsFromPlayer(EntityPlayer player){ + removeWingsFromPlayer(player, player.worldObj.isRemote); + } + + /** + * Adds the Player to the List of Players that have Wings + * @param player The Player + */ + public static void addWingsToPlayer(EntityPlayer player){ + wingedPlayers.add(player.getUniqueID()+(player.worldObj.isRemote ? "-Remote" : "")); + } + + @Override + public EnumRarity getRarity(ItemStack stack){ + return EnumRarity.epic; + } + + @Override + @SuppressWarnings("unchecked") + @SideOnly(Side.CLIENT) + public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean isHeld){ + ItemUtil.addInformation(this, list, 1, ""); + } + + @Override + public IIcon getIcon(ItemStack stack, int pass){ + return this.itemIcon; + } + + @Override + @SideOnly(Side.CLIENT) + public void registerIcons(IIconRegister iconReg){ + this.itemIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getName()); + } + + @Override + public String getName(){ + return "itemWingsOfTheBats"; + } + + /** + * Checks if the Player has Wings in its Inventory + * + * @param player The Player + * @return If the Player has Wings + */ + public static boolean hasWingItem(EntityPlayer player){ + for(int i = 0; i < player.inventory.getSizeInventory(); i++){ + if(player.inventory.getStackInSlot(i) != null && player.inventory.getStackInSlot(i).getItem() instanceof ItemWingsOfTheBats){ + return true; + } + } + return false; + } +}