Added flight limit to wings of the bats

This commit is contained in:
Ellpeck 2016-11-22 18:26:29 +01:00
parent c7912a01d7
commit 2671cad420
4 changed files with 113 additions and 82 deletions

View file

@ -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<PlayerSave> data = WorldData.getWorldUnspecificData().playerSaveData;
//Get Data from existing data
for(PlayerSave save : data){
if(save.id != null && save.id.equals(id)){
ConcurrentHashMap<UUID, PlayerSave> 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){

View file

@ -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<Integer, WorldData> WORLD_DATA = new ConcurrentHashMap<Integer, WorldData>();
public final ConcurrentSet<Network> laserRelayNetworks = new ConcurrentSet<Network>();
public final ArrayList<PlayerSave> playerSaveData = new ArrayList<PlayerSave>();
public final ConcurrentHashMap<UUID, PlayerSave> playerSaveData = new ConcurrentHashMap<UUID, PlayerSave>();
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);

View file

@ -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);
}

View file

@ -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
* <p>
* Saves Remote Players separately to make de-synced Event Calling
* not bug out Capabilities when taking off the Wings
* <p>
* (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<String> WINGED_PLAYERS = new ArrayList<String>();
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" : ""));
@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);
}
/**
* 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" : ""));
}
/**
@ -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(!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){
//Make the Player actually winged
ItemWingsOfTheBats.addWingsToPlayer(player);
data.hasBatWings = true;
shouldSend = true;
}
}
//If Player is (or should be) winged
else{
if(wingsEquipped){
//Allow the Player to fly when he has Wings equipped
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{
if(data.hasBatWings){
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;
}
}