diff options
Diffstat (limited to 'src/main/java/com')
4 files changed, 156 insertions, 35 deletions
diff --git a/src/main/java/com/github/technus/tectech/TecTech.java b/src/main/java/com/github/technus/tectech/TecTech.java index 9d3273f10e..76cdc56060 100644 --- a/src/main/java/com/github/technus/tectech/TecTech.java +++ b/src/main/java/com/github/technus/tectech/TecTech.java @@ -4,6 +4,7 @@ import com.github.technus.tectech.loader.MainLoader; import com.github.technus.tectech.loader.TecTechConfig; import com.github.technus.tectech.mechanics.ConvertFloat; import com.github.technus.tectech.mechanics.ConvertInteger; +import com.github.technus.tectech.mechanics.PlayerPersistence; import com.github.technus.tectech.mechanics.anomaly.AnomalyHandler; import com.github.technus.tectech.chunkData.ChunkDataHandler; import com.github.technus.tectech.mechanics.elementalMatter.core.commands.GiveEM; @@ -44,6 +45,7 @@ public class TecTech { public static ChunkDataHandler chunkDataHandler; public static AnomalyHandler anomalyHandler; + public static PlayerPersistence playerPersistence; /** * For Loader.isModLoaded checks during the runtime @@ -189,6 +191,8 @@ public class TecTech { MainLoader.postLoad(); chunkDataHandler.registerChunkMetaDataHandler(anomalyHandler=new AnomalyHandler()); + + playerPersistence=new PlayerPersistence("tec"); } @Mod.EventHandler diff --git a/src/main/java/com/github/technus/tectech/Util.java b/src/main/java/com/github/technus/tectech/Util.java index 53e9627187..6a8ab2d57e 100644 --- a/src/main/java/com/github/technus/tectech/Util.java +++ b/src/main/java/com/github/technus/tectech/Util.java @@ -3,6 +3,8 @@ package com.github.technus.tectech; import com.github.technus.tectech.thing.casing.TT_Container_Casings; import com.github.technus.tectech.thing.metaTileEntity.IFrontRotation; import com.github.technus.tectech.thing.metaTileEntity.multi.base.IHatchAdder; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.ObfuscationReflectionHelper; import cpw.mods.fml.common.registry.GameRegistry; import gregtech.api.GregTech_API; import gregtech.api.interfaces.metatileentity.IMetaTileEntity; @@ -12,19 +14,27 @@ import gregtech.api.util.GT_OreDictUnificator; import gregtech.api.util.GT_Utility; import net.minecraft.block.Block; import net.minecraft.block.material.Material; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.inventory.Container; import net.minecraft.inventory.ICrafting; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.MinecraftServer; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.World; +import net.minecraft.world.storage.IPlayerFileData; +import net.minecraft.world.storage.SaveHandler; import net.minecraftforge.fluids.FluidStack; import org.apache.commons.lang3.StringUtils; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.lang.reflect.Field; import java.util.*; import java.util.regex.Matcher; @@ -1457,4 +1467,94 @@ public final class Util { int z=chunk.getCenterZPosition(); return world.checkChunksExist(x, 0, z, x, 0, z); } + + public static NBTTagCompound getPlayerData(EntityPlayer player,String extension) { + try { + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { + if (player != null) { + IPlayerFileData playerNBTManagerObj = MinecraftServer.getServer().worldServerForDimension(0).getSaveHandler().getSaveHandler(); + SaveHandler sh = (SaveHandler)playerNBTManagerObj; + File dir = ObfuscationReflectionHelper.getPrivateValue(SaveHandler.class, sh, new String[]{"playersDirectory", "field_75771_c"}); + String id1=player.getUniqueID().toString(); + NBTTagCompound tagCompound=read(new File(dir, id1 + "."+extension)); + if(tagCompound!=null){ + return tagCompound; + } + tagCompound=readBackup(new File(dir, id1 + "."+extension+"_bak")); + if(tagCompound!=null){ + return tagCompound; + } + String id2=UUID.nameUUIDFromBytes(player.getCommandSenderName().getBytes()).toString(); + tagCompound=read(new File(dir, id2 + "."+extension)); + if(tagCompound!=null){ + return tagCompound; + } + tagCompound=readBackup(new File(dir, id2 + "."+extension+"_bak")); + if(tagCompound!=null){ + return tagCompound; + } + } + } + } catch (Exception var9) { + TecTech.LOGGER.fatal("Error reading data for: "+player.getCommandSenderName()); + } + return new NBTTagCompound(); + } + + public static void savePlayerFile(EntityPlayer player,String extension, NBTTagCompound data) { + try { + if (FMLCommonHandler.instance().getEffectiveSide().isServer()) { + if (player != null) { + IPlayerFileData playerNBTManagerObj = MinecraftServer.getServer().worldServerForDimension(0).getSaveHandler().getSaveHandler(); + SaveHandler sh = (SaveHandler)playerNBTManagerObj; + File dir = ObfuscationReflectionHelper.getPrivateValue(SaveHandler.class, sh, new String[]{"playersDirectory", "field_75771_c"}); + String id1=player.getUniqueID().toString(); + write(new File(dir, id1 + "."+extension),data); + write(new File(dir, id1 + "."+extension+"_bak"),data); + String id2=UUID.nameUUIDFromBytes(player.getCommandSenderName().getBytes()).toString(); + write(new File(dir, id2 + "."+extension),data); + write(new File(dir, id2 + "."+extension+"_bak"),data); + } + } + } catch (Exception var10) { + TecTech.LOGGER.fatal("Error saving data for: "+player.getCommandSenderName()); + } + } + + private static NBTTagCompound read(File file){ + if (file != null && file.exists()) { + try(FileInputStream fileInputStream= new FileInputStream(file)) { + return CompressedStreamTools.readCompressed(fileInputStream); + } catch (Exception var9) { + TecTech.LOGGER.error("Cannot read NBT File: "+file.getAbsolutePath()); + } + } + return null; + } + + private static NBTTagCompound readBackup(File file){ + if (file != null && file.exists()) { + try(FileInputStream fileInputStream= new FileInputStream(file)) { + return CompressedStreamTools.readCompressed(fileInputStream); + } catch (Exception var9) { + TecTech.LOGGER.error("Cannot read NBT File: "+file.getAbsolutePath()); + return new NBTTagCompound(); + } + } + return null; + } + + private static void write(File file,NBTTagCompound tagCompound){ + if (file != null) { + if(tagCompound==null){ + if(file.exists()) file.delete(); + }else { + try(FileOutputStream fileOutputStream= new FileOutputStream(file)) { + CompressedStreamTools.writeCompressed(tagCompound,fileOutputStream); + } catch (Exception var9) { + TecTech.LOGGER.error("Cannot write NBT File: "+file.getAbsolutePath()); + } + } + } + } } diff --git a/src/main/java/com/github/technus/tectech/mechanics/PlayerPersistence.java b/src/main/java/com/github/technus/tectech/mechanics/PlayerPersistence.java new file mode 100644 index 0000000000..db57b1e563 --- /dev/null +++ b/src/main/java/com/github/technus/tectech/mechanics/PlayerPersistence.java @@ -0,0 +1,42 @@ +package com.github.technus.tectech.mechanics; + +import com.github.technus.tectech.Util; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; + +import java.util.HashMap; +import java.util.UUID; + +public class PlayerPersistence { + private final HashMap<UUID, NBTTagCompound> map=new HashMap<>(); + private final String extension; + + public PlayerPersistence(String extension) { + this.extension = extension; + } + + public final NBTTagCompound getData(EntityPlayer player){ + NBTTagCompound tag=map.get(player.getUniqueID()); + if(tag!=null){ + return tag; + } + tag=map.get(UUID.fromString(player.getCommandSenderName())); + if(tag!=null){ + return tag; + } + tag=Util.getPlayerData(player,extension); + if(tag!=null){ + map.put(player.getUniqueID(),tag); + map.put(UUID.fromString(player.getCommandSenderName()),tag); + } + return tag; + } + + public final void saveData(EntityPlayer player){ + NBTTagCompound tag=map.get(player.getUniqueID()); + if(tag==null){ + tag=map.get(UUID.fromString(player.getCommandSenderName())); + } + Util.savePlayerFile(player,extension,tag); + } +} diff --git a/src/main/java/com/github/technus/tectech/mechanics/anomaly/AnomalyHandler.java b/src/main/java/com/github/technus/tectech/mechanics/anomaly/AnomalyHandler.java index ce79c42003..1692037d6b 100644 --- a/src/main/java/com/github/technus/tectech/mechanics/anomaly/AnomalyHandler.java +++ b/src/main/java/com/github/technus/tectech/mechanics/anomaly/AnomalyHandler.java @@ -6,31 +6,19 @@ import com.github.technus.tectech.chunkData.IChunkMetaDataHandler; import com.github.technus.tectech.loader.network.ChunkDataMessage; import com.github.technus.tectech.loader.network.NetworkDispatcher; import com.github.technus.tectech.mechanics.elementalMatter.definitions.complex.atom.dAtomDefinition; -import cpw.mods.fml.common.eventhandler.EventPriority; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.TickEvent; -import crazypants.util.BaublesUtil; -import crazypants.util.GalacticraftUtil; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityClientPlayerMP; -import net.minecraft.entity.item.EntityItem; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.inventory.IInventory; -import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.ChunkCoordIntPair; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; -import net.minecraftforge.common.util.FakePlayer; -import net.minecraftforge.event.entity.player.PlayerDropsEvent; -import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.world.ChunkEvent; import java.util.ArrayList; import java.util.HashMap; -import java.util.ListIterator; public class AnomalyHandler implements IChunkMetaDataHandler { private static final double SWAP_THRESHOLD = dAtomDefinition.getSomethingHeavy().getMass() * 10000D; @@ -142,6 +130,16 @@ public class AnomalyHandler implements IChunkMetaDataHandler { } @Override + public void tickRender(HashMap<Integer, ChunkDataHandler.ChunkHashMap> data, TickEvent.RenderTickEvent aEvent) { + EntityClientPlayerMP player=Minecraft.getMinecraft().thePlayer; + if(player!=null) { + player.setAngles((TecTech.RANDOM.nextFloat() - .5F)*4, (TecTech.RANDOM.nextFloat() - .5F)*4); + player.setVelocity(TecTech.RANDOM.nextFloat()*2-1,TecTech.RANDOM.nextFloat()*2-1,TecTech.RANDOM.nextFloat()*2-1); + player.setInvisible(TecTech.RANDOM.nextBoolean()); + } + } + + @Override public void pullData(ChunkEvent.Load aEvent) { NetworkDispatcher.INSTANCE.sendToServer(new ChunkDataMessage.ChunkDataQuery(aEvent, this)); } @@ -179,27 +177,4 @@ public class AnomalyHandler implements IChunkMetaDataHandler { } TecTech.chunkDataHandler.getChunkData(this, world).markForTransmissionToClient(chunk); } - - @SubscribeEvent - public void onPlayerClone(PlayerEvent.Clone evt) { - if (evt.wasDeath && !evt.isCanceled()) { - if (evt.original != null && evt.entityPlayer != null && !(evt.entityPlayer instanceof FakePlayer)) { - int i; - ItemStack item; - for (i = 0; i < evt.original.field_71071_by.field_70462_a.length; ++i) { - item = evt.original.field_71071_by.field_70462_a[i]; - if (this.isSoulBound(item) && this.addToPlayerInventory(evt.entityPlayer, item)) { - evt.original.field_71071_by.field_70462_a[i] = null; - } - } - - for (i = 0; i < evt.original.field_71071_by.field_70460_b.length; ++i) { - item = evt.original.field_71071_by.field_70460_b[i]; - if (this.isSoulBound(item) && this.addToPlayerInventory(evt.entityPlayer, item)) { - evt.original.field_71071_by.field_70460_b[i] = null; - } - } - } - } - } } |