From 2671cad420f3858de425418618b25653f92bbabf Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 22 Nov 2016 18:26:29 +0100 Subject: [PATCH] Added flight limit to wings of the bats --- .../mod/data/PlayerData.java | 22 ++- .../actuallyadditions/mod/data/WorldData.java | 7 +- .../mod/items/ItemPotionRing.java | 4 +- .../mod/items/ItemWingsOfTheBats.java | 162 ++++++++++-------- 4 files changed, 113 insertions(+), 82 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java b/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java index 25589e0ed..0f0c1d61c 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/data/PlayerData.java @@ -20,24 +20,24 @@ import net.minecraft.nbt.NBTTagString; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -import java.util.List; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; public final class PlayerData{ public static PlayerSave getDataFromPlayer(UUID id){ - List data = WorldData.getWorldUnspecificData().playerSaveData; - //Get Data from existing data - for(PlayerSave save : data){ - if(save.id != null && save.id.equals(id)){ + ConcurrentHashMap data = WorldData.getWorldUnspecificData().playerSaveData; + if(data.containsKey(id)){ + PlayerSave save = data.get(id); + if(save != null && save.id != null && save.id.equals(id)){ return save; } } //Add Data if none is existant - PlayerSave aSave = new PlayerSave(id); - data.add(aSave); - return aSave; + PlayerSave save = new PlayerSave(id); + data.put(id, save); + return save; } public static PlayerSave getDataFromPlayer(EntityPlayer player){ @@ -51,6 +51,8 @@ public final class PlayerData{ public boolean displayTesla; public boolean bookGottenAlready; public boolean didBookTutorial; + public boolean hasBatWings; + public int batWingsFlyTime; public IBookletPage[] bookmarks = new IBookletPage[12]; @@ -65,6 +67,8 @@ public final class PlayerData{ this.displayTesla = compound.getBoolean("DisplayTesla"); this.bookGottenAlready = compound.getBoolean("BookGotten"); this.didBookTutorial = compound.getBoolean("DidTutorial"); + this.hasBatWings = compound.getBoolean("HasBatWings"); + this.batWingsFlyTime = compound.getInteger("BatWingsFlyTime"); NBTTagList bookmarks = compound.getTagList("Bookmarks", 8); for(int i = 0; i < bookmarks.tagCount(); i++){ @@ -80,6 +84,8 @@ public final class PlayerData{ compound.setBoolean("DisplayTesla", this.displayTesla); compound.setBoolean("BookGotten", this.bookGottenAlready); compound.setBoolean("DidTutorial", this.didBookTutorial); + compound.setBoolean("HasBatWings", this.hasBatWings); + compound.setInteger("BatWingsFlyTime", this.batWingsFlyTime); NBTTagList bookmarks = new NBTTagList(); for(IBookletPage bookmark : this.bookmarks){ diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/data/WorldData.java b/src/main/java/de/ellpeck/actuallyadditions/mod/data/WorldData.java index cdb7f377f..29cada13b 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/data/WorldData.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/data/WorldData.java @@ -15,6 +15,7 @@ import de.ellpeck.actuallyadditions.mod.data.PlayerData.PlayerSave; import de.ellpeck.actuallyadditions.mod.misc.LaserRelayConnectionHandler; import de.ellpeck.actuallyadditions.mod.util.ModUtil; import io.netty.util.internal.ConcurrentSet; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -36,7 +37,7 @@ public class WorldData{ public static final String DATA_TAG = ModUtil.MOD_ID+"data"; private static final ConcurrentHashMap WORLD_DATA = new ConcurrentHashMap(); public final ConcurrentSet laserRelayNetworks = new ConcurrentSet(); - public final ArrayList playerSaveData = new ArrayList(); + public final ConcurrentHashMap playerSaveData = new ConcurrentHashMap(); private final ISaveHandler handler; private final int dimension; @@ -149,7 +150,7 @@ public class WorldData{ PlayerSave save = new PlayerSave(id); save.readFromNBT(data); - this.playerSaveData.add(save); + this.playerSaveData.put(id, save); } } @@ -163,7 +164,7 @@ public class WorldData{ //Player Data NBTTagList playerList = new NBTTagList(); - for(PlayerSave save : this.playerSaveData){ + for(PlayerSave save : this.playerSaveData.values()){ NBTTagCompound player = new NBTTagCompound(); player.setUniqueId("UUID", save.id); diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemPotionRing.java b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemPotionRing.java index b0df60b67..56f5adea8 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemPotionRing.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemPotionRing.java @@ -60,13 +60,13 @@ public class ItemPotionRing extends ItemBase implements IColorProvidingItem, IDi @Override public double getDurabilityForDisplay(ItemStack stack){ - double diff = MAX_BLAZE-this.getStoredBlaze(stack); + double diff = MAX_BLAZE-getStoredBlaze(stack); return diff/MAX_BLAZE; } @Override public int getRGBDurabilityForDisplay(ItemStack stack){ - int curr = this.getStoredBlaze(stack); + int curr = getStoredBlaze(stack); return MathHelper.hsvToRGB(Math.max(0.0F, (float)curr/MAX_BLAZE)/3.0F, 1.0F, 1.0F); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemWingsOfTheBats.java b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemWingsOfTheBats.java index 77e363cd2..1f5a655d5 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemWingsOfTheBats.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/items/ItemWingsOfTheBats.java @@ -11,34 +11,27 @@ package de.ellpeck.actuallyadditions.mod.items; import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues; +import de.ellpeck.actuallyadditions.mod.data.PlayerData; import de.ellpeck.actuallyadditions.mod.items.base.ItemBase; import de.ellpeck.actuallyadditions.mod.items.metalists.TheMiscItems; +import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper; import de.ellpeck.actuallyadditions.mod.util.StackUtil; +import net.minecraft.client.Minecraft; import net.minecraft.entity.passive.EntityBat; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.EnumRarity; import net.minecraft.item.ItemStack; +import net.minecraft.util.math.MathHelper; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.entity.living.LivingDropsEvent; import net.minecraftforge.event.entity.living.LivingEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -import net.minecraftforge.fml.common.gameevent.PlayerEvent; - -import java.util.ArrayList; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; public class ItemWingsOfTheBats extends ItemBase{ - /** - * 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 separately to make de-synced Event Calling - * not bug out Capabilities when taking off the Wings - *

- * (Partially excerpted from Botania's Wing System by Vazkii (as I had fiddled around with the system and couldn't make it work) with permission, thanks!) - */ - public static final ArrayList WINGED_PLAYERS = new ArrayList(); + private static final int MAX_FLY_TIME = 800; public ItemWingsOfTheBats(String name){ super(name); @@ -47,40 +40,35 @@ public class ItemWingsOfTheBats extends ItemBase{ MinecraftForge.EVENT_BUS.register(this); } - /** - * Checks if the Player is winged - * - * @param player The Player - * @return Winged? - */ - public static boolean isPlayerWinged(EntityPlayer player){ - return WINGED_PLAYERS.contains(player.getUniqueID()+(player.worldObj.isRemote ? "-Remote" : "")); + @Override + public boolean showDurabilityBar(ItemStack stack){ + return true; } - /** - * Same as above, but Remote Checking is done automatically - */ - public static void removeWingsFromPlayer(EntityPlayer player){ - removeWingsFromPlayer(player, player.worldObj.isRemote); + @Override + @SideOnly(Side.CLIENT) + public double getDurabilityForDisplay(ItemStack stack){ + PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().thePlayer); + if(data != null){ + double diff = MAX_FLY_TIME-data.batWingsFlyTime; + return 1-(diff/MAX_FLY_TIME); + } + else{ + return super.getDurabilityForDisplay(stack); + } } - /** - * 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){ - WINGED_PLAYERS.remove(player.getUniqueID()+(worldRemote ? "-Remote" : "")); - } - - /** - * Adds the Player to the List of Players that have Wings - * - * @param player The Player - */ - public static void addWingsToPlayer(EntityPlayer player){ - WINGED_PLAYERS.add(player.getUniqueID()+(player.worldObj.isRemote ? "-Remote" : "")); + @Override + @SideOnly(Side.CLIENT) + public int getRGBDurabilityForDisplay(ItemStack stack){ + PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().thePlayer); + if(data != null){ + int curr = data.batWingsFlyTime; + return MathHelper.hsvToRGB(Math.max(0.0F, 1-(float)curr/MAX_FLY_TIME)/3.0F, 1.0F, 1.0F); + } + else{ + return super.getRGBDurabilityForDisplay(stack); + } } /** @@ -98,13 +86,6 @@ public class ItemWingsOfTheBats extends ItemBase{ return StackUtil.getNull(); } - @SubscribeEvent - public void onLogOutEvent(PlayerEvent.PlayerLoggedOutEvent event){ - //Remove Player from Wings' Fly Permission List - ItemWingsOfTheBats.removeWingsFromPlayer(event.player, true); - ItemWingsOfTheBats.removeWingsFromPlayer(event.player, false); - } - @SubscribeEvent public void onEntityDropEvent(LivingDropsEvent event){ if(event.getEntityLiving().worldObj != null && !event.getEntityLiving().worldObj.isRemote && event.getSource().getEntity() instanceof EntityPlayer){ @@ -121,29 +102,72 @@ public class ItemWingsOfTheBats extends ItemBase{ public void livingUpdateEvent(LivingEvent.LivingUpdateEvent event){ if(event.getEntityLiving() instanceof EntityPlayer){ EntityPlayer player = (EntityPlayer)event.getEntityLiving(); - boolean wingsEquipped = StackUtil.isValid(ItemWingsOfTheBats.getWingItem(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 - player.capabilities.allowFlying = true; + if(!player.capabilities.isCreativeMode){ + PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player); + + if(!player.worldObj.isRemote){ + boolean shouldDeduct = false; + boolean shouldSend = false; + + boolean wingsEquipped = StackUtil.isValid(ItemWingsOfTheBats.getWingItem(player)); + if(!data.hasBatWings){ + if(data.batWingsFlyTime <= 0){ + if(wingsEquipped){ + data.hasBatWings = true; + shouldSend = true; + } + } + else{ + shouldDeduct = true; + } + } + else{ + if(wingsEquipped && data.batWingsFlyTime < MAX_FLY_TIME){ + player.capabilities.allowFlying = true; + + if(player.capabilities.isFlying){ + data.batWingsFlyTime++; + + if(player.worldObj.getTotalWorldTime()%10 == 0){ + shouldSend = true; + } + } + else{ + shouldDeduct = true; + } + } + else{ + data.hasBatWings = false; + shouldSend = true; + + player.capabilities.allowFlying = false; + player.capabilities.isFlying = false; + player.capabilities.disableDamage = false; + } + } + + if(shouldDeduct){ + if(data.batWingsFlyTime >= 0){ + data.batWingsFlyTime = Math.max(0, data.batWingsFlyTime-5); + } + + if(player.worldObj.getTotalWorldTime()%10 == 0){ + shouldSend = true; + } + } + + if(shouldSend){ + PacketHandlerHelper.sendPlayerDataPacket(player, false, true); + } } else{ - //Make the Player not winged - ItemWingsOfTheBats.removeWingsFromPlayer(player); - //Reset Player's Values - if(!player.capabilities.isCreativeMode){ + if(data.hasBatWings){ + player.capabilities.allowFlying = true; + } + else{ player.capabilities.allowFlying = false; player.capabilities.isFlying = false; - //Enables Fall Damage again (Automatically gets disabled for some reason) player.capabilities.disableDamage = false; } }