From 0e753cbdd3ede4f3aead046014ee408ce5cf62d1 Mon Sep 17 00:00:00 2001 From: Azanor Date: Wed, 9 Jul 2014 10:49:23 +0200 Subject: [PATCH] Initial changes for MC 1.7.10 Not backwards compatible atm. Not sure if I want it to be. --- build.gradle | 4 +- src/main/java/baubles/common/Baubles.java | 9 +- .../container/ContainerPlayerExpanded.java | 2 +- .../common/container/InventoryBaubles.java | 80 +++++--- .../common/event/EventHandlerEntity.java | 54 +++-- .../baubles/common/lib/PlayerHandler.java | 192 +++++++----------- .../common/network/EventHandlerNetwork.java | 30 --- src/main/resources/changelog.txt | 5 + 8 files changed, 173 insertions(+), 203 deletions(-) delete mode 100644 src/main/java/baubles/common/network/EventHandlerNetwork.java diff --git a/build.gradle b/build.gradle index 15f8fde..92f34a4 100644 --- a/build.gradle +++ b/build.gradle @@ -17,12 +17,12 @@ buildscript { apply plugin: 'forge' -version = "1.7.2-1.0.0.16" +version = "1.7.10-1.0.1.0" group= "com.azanor.baubles" archivesBaseName = "Baubles" minecraft { - version = "1.7.2-10.12.1.1110" + version = "1.7.10-10.13.0.1177" assetDir = "eclipse/assets" } diff --git a/src/main/java/baubles/common/Baubles.java b/src/main/java/baubles/common/Baubles.java index 66772d9..f3cabfe 100644 --- a/src/main/java/baubles/common/Baubles.java +++ b/src/main/java/baubles/common/Baubles.java @@ -8,9 +8,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import baubles.common.event.EventHandlerEntity; -import baubles.common.network.EventHandlerNetwork; import baubles.common.network.PacketHandler; -import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.Mod.EventHandler; import cpw.mods.fml.common.Mod.Instance; @@ -20,13 +18,13 @@ import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.network.NetworkRegistry; -@Mod(modid = Baubles.MODID, name = Baubles.MODNAME, version = Baubles.VERSION, dependencies="required-after:Forge@[10.12.1.1110,);") +@Mod(modid = Baubles.MODID, name = Baubles.MODNAME, version = Baubles.VERSION, dependencies="required-after:Forge@[10.13.0.1177,);") public class Baubles { public static final String MODID = "Baubles"; public static final String MODNAME = "Baubles"; - public static final String VERSION = "1.0.0.16"; + public static final String VERSION = "1.0.1.0"; @SidedProxy(clientSide = "baubles.client.ClientProxy", serverSide = "baubles.common.CommonProxy") public static CommonProxy proxy; @@ -34,8 +32,6 @@ public class Baubles { @Instance(value=Baubles.MODID) public static Baubles instance; - - public EventHandlerNetwork networkEventHandler; public EventHandlerEntity entityEventHandler; public File modDir; @@ -60,7 +56,6 @@ public class Baubles { entityEventHandler = new EventHandlerEntity(); MinecraftForge.EVENT_BUS.register(entityEventHandler); - FMLCommonHandler.instance().bus().register(new EventHandlerNetwork()); proxy.registerHandlers(); ///////////////////// diff --git a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java index e3dab06..a17070b 100644 --- a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java +++ b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java @@ -118,7 +118,7 @@ public class ContainerPlayerExpanded extends Container this.craftResult.setInventorySlotContents(0, (ItemStack)null); if (!player.worldObj.isRemote) { PlayerHandler.setPlayerBaubles(player, baubles); - PlayerHandler.savePlayerBaubles(player); +// PlayerHandler.savePlayerBaubles(player); } } diff --git a/src/main/java/baubles/common/container/InventoryBaubles.java b/src/main/java/baubles/common/container/InventoryBaubles.java index cebbd92..74164de 100644 --- a/src/main/java/baubles/common/container/InventoryBaubles.java +++ b/src/main/java/baubles/common/container/InventoryBaubles.java @@ -94,27 +94,31 @@ public class InventoryBaubles implements IInventory { ItemStack itemstack; if (this.stackList[par1].stackSize <= par2) { - itemstack = this.stackList[par1]; + itemstack = this.stackList[par1]; this.stackList[par1] = null; - - if (itemstack !=null && itemstack.getItem() instanceof IBauble) { - ((IBauble)itemstack.getItem()).onUnequipped(itemstack, player.get()); + + if (itemstack != null && itemstack.getItem() instanceof IBauble) { + ((IBauble) itemstack.getItem()).onUnequipped(itemstack, + player.get()); } - - if (eventHandler!=null) this.eventHandler.onCraftMatrixChanged(this); + + if (eventHandler != null) + this.eventHandler.onCraftMatrixChanged(this); syncSlotToClients(par1); return itemstack; } else { itemstack = this.stackList[par1].splitStack(par2); - + if (this.stackList[par1].stackSize == 0) { this.stackList[par1] = null; } - if (itemstack !=null && itemstack.getItem() instanceof IBauble) { - ((IBauble)itemstack.getItem()).onUnequipped(itemstack, player.get()); + if (itemstack != null && itemstack.getItem() instanceof IBauble) { + ((IBauble) itemstack.getItem()).onUnequipped(itemstack, + player.get()); } - if (eventHandler!=null) this.eventHandler.onCraftMatrixChanged(this); + if (eventHandler != null) + this.eventHandler.onCraftMatrixChanged(this); syncSlotToClients(par1); return itemstack; } @@ -130,10 +134,11 @@ public class InventoryBaubles implements IInventory { @Override public void setInventorySlotContents(int par1, ItemStack stack) { this.stackList[par1] = stack; - if (stack !=null && stack.getItem() instanceof IBauble) { - ((IBauble)stack.getItem()).onEquipped(stack, player.get()); + if (stack != null && stack.getItem() instanceof IBauble) { + ((IBauble) stack.getItem()).onEquipped(stack, player.get()); } - if (eventHandler!=null) this.eventHandler.onCraftMatrixChanged(this); + if (eventHandler != null) + this.eventHandler.onCraftMatrixChanged(this); syncSlotToClients(par1); } @@ -150,11 +155,11 @@ public class InventoryBaubles implements IInventory { * to disk later - the game won't think it hasn't changed and skip it. */ @Override - public void markDirty() { - + public void markDirty() { try { - PlayerHandler.savePlayerBaubles(player.get()); - } catch (Exception e) { } + player.get().inventory.markDirty(); + } catch (Exception e) { + } } /** @@ -168,12 +173,12 @@ public class InventoryBaubles implements IInventory { @Override public void openInventory() { - + } @Override - public void closeInventory() { - + public void closeInventory() { + } /** @@ -201,7 +206,7 @@ public class InventoryBaubles implements IInventory { NBTTagCompound tags = player.getEntityData(); saveNBT(tags); } - + public void saveNBT(NBTTagCompound tags) { NBTTagList tagList = new NBTTagList(); NBTTagCompound invSlot; @@ -220,13 +225,15 @@ public class InventoryBaubles implements IInventory { NBTTagCompound tags = player.getEntityData(); readNBT(tags); } - + public void readNBT(NBTTagCompound tags) { NBTTagList tagList = tags.getTagList("Baubles.Inventory", 10); for (int i = 0; i < tagList.tagCount(); ++i) { - NBTTagCompound nbttagcompound = (NBTTagCompound) tagList.getCompoundTagAt(i); + NBTTagCompound nbttagcompound = (NBTTagCompound) tagList + .getCompoundTagAt(i); int j = nbttagcompound.getByte("Slot") & 255; - ItemStack itemstack = ItemStack.loadItemStackFromNBT(nbttagcompound); + ItemStack itemstack = ItemStack + .loadItemStackFromNBT(nbttagcompound); if (itemstack != null) { this.stackList[j] = itemstack; } @@ -236,25 +243,32 @@ public class InventoryBaubles implements IInventory { public void dropItems(ArrayList drops) { for (int i = 0; i < 4; ++i) { if (this.stackList[i] != null) { - EntityItem ei = new EntityItem(player.get().worldObj, player.get().posX,player.get().posY+player.get().eyeHeight,player.get().posZ,this.stackList[i].copy()); + EntityItem ei = new EntityItem(player.get().worldObj, + player.get().posX, player.get().posY + + player.get().eyeHeight, player.get().posZ, + this.stackList[i].copy()); ei.delayBeforeCanPickup = 40; float f1 = player.get().worldObj.rand.nextFloat() * 0.5F; - float f2 = player.get().worldObj.rand.nextFloat() * (float)Math.PI * 2.0F; - ei.motionX = (double)(-MathHelper.sin(f2) * f1); - ei.motionZ = (double)(MathHelper.cos(f2) * f1); - ei.motionY = 0.20000000298023224D; + float f2 = player.get().worldObj.rand.nextFloat() + * (float) Math.PI * 2.0F; + ei.motionX = (double) (-MathHelper.sin(f2) * f1); + ei.motionZ = (double) (MathHelper.cos(f2) * f1); + ei.motionY = 0.20000000298023224D; drops.add(ei); this.stackList[i] = null; syncSlotToClients(i); } } } - + public void syncSlotToClients(int slot) { try { - if (Baubles.proxy.getClientWorld()==null) { - PacketHandler.INSTANCE.sendToAll(new PacketSyncBauble(player.get(),slot)); + if (Baubles.proxy.getClientWorld() == null) { + PacketHandler.INSTANCE.sendToAll(new PacketSyncBauble(player + .get(), slot)); } - } catch (Exception e) { e.printStackTrace(); } + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/baubles/common/event/EventHandlerEntity.java b/src/main/java/baubles/common/event/EventHandlerEntity.java index c84cd71..7f6e9dd 100644 --- a/src/main/java/baubles/common/event/EventHandlerEntity.java +++ b/src/main/java/baubles/common/event/EventHandlerEntity.java @@ -6,35 +6,57 @@ import net.minecraftforge.event.entity.player.PlayerEvent; import baubles.api.IBauble; import baubles.common.container.InventoryBaubles; import baubles.common.lib.PlayerHandler; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.relauncher.Side; -public class EventHandlerEntity { +public class EventHandlerEntity { @SubscribeEvent public void playerTick(PlayerEvent.LivingUpdateEvent event) { - - //player events + + // player events if (event.entity instanceof EntityPlayer) { - EntityPlayer player = (EntityPlayer)event.entity; - + EntityPlayer player = (EntityPlayer) event.entity; + InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player); - for (int a=0;a playerBaubles = new HashMap(); - + + private static HashMap playerBaubles = new HashMap(); + public static void clearPlayerBaubles(EntityPlayer player) { playerBaubles.remove(player.getCommandSenderName()); } - + public static InventoryBaubles getPlayerBaubles(EntityPlayer player) { if (!playerBaubles.containsKey(player.getCommandSenderName())) { InventoryBaubles inventory = new InventoryBaubles(player); @@ -26,141 +28,103 @@ public class PlayerHandler { } return playerBaubles.get(player.getCommandSenderName()); } - - public static void setPlayerBaubles(EntityPlayer player, InventoryBaubles inventory) { + + public static void setPlayerBaubles(EntityPlayer player, + InventoryBaubles inventory) { playerBaubles.put(player.getCommandSenderName(), inventory); } - - public static File getFileFromPlayer(EntityPlayer player) - { - try { - File playersDirectory = new File(player.worldObj.getSaveHandler().getWorldDirectory(), "players"); - return new File(playersDirectory, player.getCommandSenderName() + ".baub"); - } catch (Exception e) { e.printStackTrace(); } - return null; - } - - public static File getBackupFileFromPlayer(EntityPlayer player) - { - try { - File playersDirectory = new File(player.worldObj.getSaveHandler().getWorldDirectory(), "players"); - return new File(playersDirectory, player.getCommandSenderName() + ".baubback"); - } catch (Exception e) { e.printStackTrace(); } - return null; - } - - public static void loadPlayerBaubles(EntityPlayer player) { - if (player!=null && !player.worldObj.isRemote) { - try - { + + public static void loadPlayerBaubles(EntityPlayer player, File file1, File file2) { + if (player != null && !player.worldObj.isRemote) { + try { NBTTagCompound data = null; - boolean save = false; - File file = getFileFromPlayer(player); - if (file != null && file.exists()) - { - try { - FileInputStream fileinputstream = new FileInputStream(file); - data = CompressedStreamTools.readCompressed(fileinputstream); + boolean save = false; + if (file1 != null && file1.exists()) { + try { + FileInputStream fileinputstream = new FileInputStream( + file1); + data = CompressedStreamTools + .readCompressed(fileinputstream); fileinputstream.close(); } catch (Exception e) { e.printStackTrace(); } - } - - if (file == null || !file.exists() || data == null || data.hasNoTags()) - { - Baubles.log.warn("Data not found for "+player.getCommandSenderName()+". Trying to load backup data."); - file = getBackupFileFromPlayer(player); - if (file != null && file.exists()) - { - try { - FileInputStream fileinputstream = new FileInputStream(file); - data = CompressedStreamTools.readCompressed(fileinputstream); + } + + if (file1 == null || !file1.exists() || data == null + || data.hasNoTags()) { + Baubles.log.warn("Data not found for " + + player.getCommandSenderName() + + ". Trying to load backup data."); + if (file2 != null && file2.exists()) { + try { + FileInputStream fileinputstream = new FileInputStream( + file2); + data = CompressedStreamTools + .readCompressed(fileinputstream); fileinputstream.close(); save = true; } catch (Exception e) { e.printStackTrace(); } - } - } - - if (file == null || !file.exists() || data == null || data.hasNoTags()) - { //legacy data - Baubles.log.warn("Data not found for "+player.getCommandSenderName()+". Trying to load legacy data."); - data = player.getEntityData(); - save = true; - } - - if (data!=null) { - InventoryBaubles inventory = new InventoryBaubles(player); - inventory.readNBT(data); - playerBaubles.put(player.getCommandSenderName(), inventory); - if (save) savePlayerBaubles(player); - } - } - catch (Exception exception1) - { - Baubles.log.fatal("Error loading baubles inventory"); - exception1.printStackTrace(); - } - } + } + } + + if (data != null) { + InventoryBaubles inventory = new InventoryBaubles(player); + inventory.readNBT(data); + playerBaubles.put(player.getCommandSenderName(), inventory); + if (save) + savePlayerBaubles(player, file1, file2); + } + } catch (Exception exception1) { + Baubles.log.fatal("Error loading baubles inventory"); + exception1.printStackTrace(); + } + } } - - public static void savePlayerBaubles(EntityPlayer player) { - if (player!=null && !player.worldObj.isRemote) { - try - { - File file2 = getBackupFileFromPlayer(player); - if (file2 != null && file2.exists()) - { - try { - file2.delete(); - } - catch (Exception e) { - Baubles.log.error("Could not delete backup file for player "+player.getCommandSenderName()); + + public static void savePlayerBaubles(EntityPlayer player, File file1, File file2) { + if (player != null && !player.worldObj.isRemote) { + try { + if (file1 != null && file1.exists()) { + try { + Files.copy(file1, file2); + } catch (Exception e) { + Baubles.log + .error("Could not backup old baubles file for player " + + player.getCommandSenderName()); } - } - - File file1 = getFileFromPlayer(player); - file2 = getBackupFileFromPlayer(player); - if (file1!=null && file1.exists()) { - try { - file1.renameTo(file2); - } - catch (Exception e) { - Baubles.log.error("Could not backup old baubles file for player "+player.getCommandSenderName()); - } - } - - file1 = getFileFromPlayer(player); - try { - if (file1 != null ) - { + } + + try { + if (file1 != null) { InventoryBaubles inventory = getPlayerBaubles(player); NBTTagCompound data = new NBTTagCompound(); inventory.saveNBT(data); - - FileOutputStream fileoutputstream = new FileOutputStream(file1); - CompressedStreamTools.writeCompressed(data, fileoutputstream); - fileoutputstream.close(); - + + FileOutputStream fileoutputstream = new FileOutputStream( + file1); + CompressedStreamTools.writeCompressed(data, + fileoutputstream); + fileoutputstream.close(); + } } catch (Exception e) { - Baubles.log.error("Could not save baubles file for player "+player.getCommandSenderName()); + Baubles.log.error("Could not save baubles file for player " + + player.getCommandSenderName()); e.printStackTrace(); if (file1.exists()) { try { file1.delete(); - } - catch (Exception e2) {} + } catch (Exception e2) { + } } } - } - catch (Exception exception1) - { - Baubles.log.fatal("Error saving baubles inventory"); - exception1.printStackTrace(); - } + } catch (Exception exception1) { + Baubles.log.fatal("Error saving baubles inventory"); + exception1.printStackTrace(); + } } } } diff --git a/src/main/java/baubles/common/network/EventHandlerNetwork.java b/src/main/java/baubles/common/network/EventHandlerNetwork.java deleted file mode 100644 index c8b0b98..0000000 --- a/src/main/java/baubles/common/network/EventHandlerNetwork.java +++ /dev/null @@ -1,30 +0,0 @@ -package baubles.common.network; - -import baubles.common.lib.PlayerHandler; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import cpw.mods.fml.common.gameevent.PlayerEvent; -import cpw.mods.fml.relauncher.Side; - -public class EventHandlerNetwork { - - @SubscribeEvent - public void playerLoggedInEvent (PlayerEvent.PlayerLoggedInEvent event) { - Side side = FMLCommonHandler.instance().getEffectiveSide(); - if (side == Side.SERVER) { - PlayerHandler.clearPlayerBaubles(event.player); - PlayerHandler.loadPlayerBaubles(event.player); - for (int a=0;a<4;a++) - PlayerHandler.getPlayerBaubles(event.player).syncSlotToClients(a); - } - } - - @SubscribeEvent - public void playerUpdateEvent (PlayerEvent event) { - Side side = FMLCommonHandler.instance().getEffectiveSide(); - if (side == Side.SERVER) { - PlayerHandler.savePlayerBaubles(event.player); - } - } - -} diff --git a/src/main/resources/changelog.txt b/src/main/resources/changelog.txt index 314fe5e..4856ebc 100644 --- a/src/main/resources/changelog.txt +++ b/src/main/resources/changelog.txt @@ -1,3 +1,8 @@ +1.0.1.0 +- update for MC 1.7.10 +- no requires Forge 10.13.0.1177 +- removed support for legacy save format and moved saving/loading to new forge events + 1.0.0.16 - switch to SimpleNetworkWrapper - now requires Forge 10.12.1.1110