Initial changes for MC 1.7.10

Not backwards compatible atm. Not sure if I want it to be.
This commit is contained in:
Azanor committed 2014-07-09 10:49:23 +02:00
1 parent 93f0f69b4f
commit 0e753cbdd3
8 files changed
+173 -203

No files matched your search

+2 -7
View File
@@ -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();
/////////////////////
@@ -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);
}
}
@@ -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<EntityItem> 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();
}
}
}
@@ -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<baubles.getSizeInventory();a++) {
if (baubles.getStackInSlot(a)!=null && baubles.getStackInSlot(a).getItem() instanceof IBauble) {
((IBauble)baubles.getStackInSlot(a).getItem()).onWornTick(baubles.getStackInSlot(a), player);
for (int a = 0; a < baubles.getSizeInventory(); a++) {
if (baubles.getStackInSlot(a) != null
&& baubles.getStackInSlot(a).getItem() instanceof IBauble) {
((IBauble) baubles.getStackInSlot(a).getItem()).onWornTick(
baubles.getStackInSlot(a), player);
}
}
}
}
@SubscribeEvent
public void playerDeath(PlayerDropsEvent event) {
if (event.entity instanceof EntityPlayer && !event.entity.worldObj.isRemote && !event.entity.worldObj.getGameRules().getGameRuleBooleanValue("keepInventory")) {
PlayerHandler.getPlayerBaubles(event.entityPlayer).dropItems(event.drops);
if (event.entity instanceof EntityPlayer
&& !event.entity.worldObj.isRemote
&& !event.entity.worldObj.getGameRules()
.getGameRuleBooleanValue("keepInventory")) {
PlayerHandler.getPlayerBaubles(event.entityPlayer).dropItems(
event.drops);
}
}
@SubscribeEvent
public void playerLoad(PlayerEvent.LoadFromFile event) {
PlayerHandler.clearPlayerBaubles(event.entityPlayer);
PlayerHandler.loadPlayerBaubles(event.entityPlayer, event.getPlayerFile("baub"), event.getPlayerFile("baubback"));
for (int a = 0; a < 4; a++)
PlayerHandler.getPlayerBaubles(event.entityPlayer)
.syncSlotToClients(a);
}
@SubscribeEvent
public void playerSave(PlayerEvent.SaveToFile event) {
PlayerHandler.savePlayerBaubles(event.entityPlayer, event.getPlayerFile("baub"), event.getPlayerFile("baubback"));
}
}
@@ -11,14 +11,16 @@ import net.minecraft.nbt.NBTTagCompound;
import baubles.common.Baubles;
import baubles.common.container.InventoryBaubles;
import com.google.common.io.Files;
public class PlayerHandler {
private static HashMap<String,InventoryBaubles> playerBaubles = new HashMap<String,InventoryBaubles>();
private static HashMap<String, InventoryBaubles> playerBaubles = new HashMap<String, InventoryBaubles>();
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();
}
}
}
}
@@ -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);
}
}
}