mirror of
https://github.com/GTNewHorizons/waila.git
synced 2026-11-23 13:40:09 +00:00
Added partial NBT syncing to the protocol.
TEs will NOT be synced by default anymore. You actually have to either request "*" to get the whole NBT, or request specific keys using "aaa.bbb", etc.
This commit is contained in:
1 parent
2dd88b9c13
commit
80e30abec3
6 files changed
+127
-18
No files matched your search
@@ -158,4 +158,12 @@ public class DataAccessorBlock implements IWailaDataAccessor {
|
||||
return ForgeDirection.getOrientation(this.getPosition().sideHit);
|
||||
}
|
||||
|
||||
public boolean isTimeElapsed(long time){
|
||||
return System.currentTimeMillis() - this.timeLastUpdate >= time;
|
||||
}
|
||||
|
||||
public void resetTimer(){
|
||||
this.timeLastUpdate = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -118,4 +118,12 @@ public class DataAccessorEntity implements IWailaEntityAccessor {
|
||||
return this.partialFrame;
|
||||
}
|
||||
|
||||
public boolean isTimeElapsed(long time){
|
||||
return System.currentTimeMillis() - this.timeLastUpdate >= time;
|
||||
}
|
||||
|
||||
public void resetTimer(){
|
||||
this.timeLastUpdate = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package mcp.mobius.waila.api.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import mcp.mobius.waila.Waila;
|
||||
@@ -60,12 +61,19 @@ public class MetaDataProvider{
|
||||
|
||||
public List<String> handleBlockTextData(ItemStack itemStack, World world, EntityPlayer player, MovingObjectPosition mop, DataAccessorBlock accessor, List<String> currenttip, Layout layout) {
|
||||
Block block = accessor.getBlock();
|
||||
int blockID = accessor.getBlockID();
|
||||
|
||||
if (accessor.getTileEntity() != null && Waila.instance.serverPresent &&
|
||||
((System.currentTimeMillis() - accessor.timeLastUpdate >= 250))){
|
||||
accessor.timeLastUpdate = System.currentTimeMillis();
|
||||
PacketDispatcher.sendPacketToServer(Packet0x01TERequest.create(world, mop));
|
||||
if (accessor.getTileEntity() != null && Waila.instance.serverPresent && accessor.isTimeElapsed(250)){
|
||||
accessor.resetTimer();
|
||||
HashSet<String> keys = new HashSet<String>();
|
||||
|
||||
if (ModuleRegistrar.instance().hasSyncedNBTKeys(block))
|
||||
keys.addAll(ModuleRegistrar.instance().getSyncedNBTKeys(block));
|
||||
|
||||
if (ModuleRegistrar.instance().hasSyncedNBTKeys(accessor.getTileEntity()))
|
||||
keys.addAll(ModuleRegistrar.instance().getSyncedNBTKeys(accessor.getTileEntity()));
|
||||
|
||||
if (keys.size() != 0)
|
||||
PacketDispatcher.sendPacketToServer(Packet0x01TERequest.create(world, mop, keys));
|
||||
}
|
||||
|
||||
/* Interface IWailaBlock */
|
||||
@@ -146,16 +154,16 @@ public class MetaDataProvider{
|
||||
|
||||
public List<String> handleEntityTextData(Entity entity, World world, EntityPlayer player, MovingObjectPosition mop, DataAccessorEntity accessor, List<String> currenttip, Layout layout) {
|
||||
|
||||
/*
|
||||
Block block = accessor.getBlock();
|
||||
int blockID = accessor.getBlockID();
|
||||
|
||||
if (accessor.getTileEntity() != null && mod_Waila.instance.serverPresent &&
|
||||
((System.currentTimeMillis() - accessor.timeLastUpdate >= 250))){
|
||||
accessor.timeLastUpdate = System.currentTimeMillis();
|
||||
PacketDispatcher.sendPacketToServer(Packet0x01TERequest.create(world, mop));
|
||||
if (accessor.getEntity() != null && Waila.instance.serverPresent && accessor.isTimeElapsed(250)){
|
||||
accessor.resetTimer();
|
||||
HashSet<String> keys = new HashSet<String>();
|
||||
|
||||
if (ModuleRegistrar.instance().hasSyncedNBTKeys(accessor.getEntity()))
|
||||
keys.addAll(ModuleRegistrar.instance().getSyncedNBTKeys(accessor.getEntity()));
|
||||
|
||||
//if (keys.size() != 0)
|
||||
// PacketDispatcher.sendPacketToServer(Packet0x01TERequest.create(world, mop, keys));
|
||||
}
|
||||
*/
|
||||
|
||||
headEntityProviders.clear();
|
||||
bodyEntityProviders.clear();
|
||||
|
||||
@@ -5,7 +5,9 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
|
||||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
import net.minecraft.util.MovingObjectPosition;
|
||||
import net.minecraft.world.World;
|
||||
@@ -17,29 +19,43 @@ public class Packet0x01TERequest {
|
||||
public int posX;
|
||||
public int posY;
|
||||
public int posZ;
|
||||
|
||||
public HashSet<String> keys = new HashSet<String> ();
|
||||
|
||||
public Packet0x01TERequest(Packet250CustomPayload packet){
|
||||
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
|
||||
keys.clear();
|
||||
try{
|
||||
this.header = inputStream.readByte();
|
||||
this.worldID = inputStream.readInt();
|
||||
this.posX = inputStream.readInt();
|
||||
this.posY = inputStream.readInt();
|
||||
this.posZ = inputStream.readInt();
|
||||
|
||||
int nkeys = inputStream.readInt();
|
||||
for (int i = 0; i < nkeys; i++)
|
||||
keys.add(Packet.readString(inputStream, 250));
|
||||
|
||||
} catch (IOException e){}
|
||||
}
|
||||
|
||||
public static Packet250CustomPayload create(World world, MovingObjectPosition mop){
|
||||
public static Packet250CustomPayload create(World world, MovingObjectPosition mop, HashSet<String> keys){
|
||||
Packet250CustomPayload packet = new Packet250CustomPayload();
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(1 + 4 + 4 + 4 + 4);
|
||||
DataOutputStream outputStream = new DataOutputStream(bos);
|
||||
|
||||
keys.add("x"); keys.add("y"); keys.add("z");
|
||||
|
||||
try{
|
||||
outputStream.writeByte(0x01);
|
||||
outputStream.writeInt(world.provider.dimensionId);
|
||||
outputStream.writeInt(mop.blockX);
|
||||
outputStream.writeInt(mop.blockY);
|
||||
outputStream.writeInt(mop.blockZ);
|
||||
outputStream.writeInt(mop.blockZ);
|
||||
outputStream.writeInt(keys.size());
|
||||
|
||||
for (String key : keys)
|
||||
Packet.writeString(key, outputStream);
|
||||
|
||||
}catch(IOException e){}
|
||||
|
||||
packet.channel = "Waila";
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.io.IOException;
|
||||
import mcp.mobius.waila.Waila;
|
||||
import mcp.mobius.waila.api.impl.ConfigHandler;
|
||||
import mcp.mobius.waila.api.impl.DataAccessorBlock;
|
||||
import mcp.mobius.waila.utils.NBTUtil;
|
||||
import mcp.mobius.waila.utils.WailaExceptionHandler;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.INetworkManager;
|
||||
@@ -45,7 +46,7 @@ public class WailaPacketHandler implements IPacketHandler {
|
||||
try{
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
entity.writeToNBT(tag);
|
||||
PacketDispatcher.sendPacketToPlayer(Packet0x02TENBTData.create(tag), player);
|
||||
PacketDispatcher.sendPacketToPlayer(Packet0x02TENBTData.create(NBTUtil.createTag(tag, castedPacket.keys)), player);
|
||||
}catch(Throwable e){
|
||||
WailaExceptionHandler.handleErr(e, entity.getClass().toString(), null);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package mcp.mobius.waila.utils;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import mcp.mobius.waila.Waila;
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTTagByte;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagInt;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.nbt.NBTTagLong;
|
||||
import net.minecraft.nbt.NBTTagShort;
|
||||
import net.minecraft.nbt.NBTTagString;
|
||||
|
||||
public class NBTUtil {
|
||||
|
||||
public static NBTBase getTag(String key, NBTTagCompound tag){
|
||||
String[] path = key.split("\\.");
|
||||
|
||||
NBTTagCompound deepTag = tag;
|
||||
for (String i : path){
|
||||
if (deepTag.hasKey(i)){
|
||||
if (deepTag.getTag(i) instanceof NBTTagCompound)
|
||||
deepTag = deepTag.getCompoundTag(i);
|
||||
else{
|
||||
return deepTag.getTag(i);
|
||||
}
|
||||
} else {
|
||||
Waila.log.log(Level.WARNING, "Leaf " + key + " not found.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return deepTag;
|
||||
}
|
||||
|
||||
public static NBTTagCompound setTag(String key, NBTTagCompound targetTag, NBTBase addedTag){
|
||||
String[] path = key.split("\\.");
|
||||
|
||||
NBTTagCompound deepTag = targetTag;
|
||||
for (int i = 0; i < path.length - 1; i++){
|
||||
if (!deepTag.hasKey(path[i]))
|
||||
deepTag.setCompoundTag(path[i], new NBTTagCompound());
|
||||
|
||||
deepTag = deepTag.getCompoundTag(path[i]);
|
||||
}
|
||||
|
||||
deepTag.setTag(path[path.length - 1], addedTag);
|
||||
|
||||
return targetTag;
|
||||
}
|
||||
|
||||
public static NBTTagCompound createTag(NBTTagCompound inTag, HashSet<String> keys){
|
||||
if (keys.contains("*")) return inTag;
|
||||
|
||||
NBTTagCompound outTag = new NBTTagCompound();
|
||||
|
||||
for (String key : keys){
|
||||
NBTBase tagToAdd = getTag(key, inTag);
|
||||
//System.out.printf("%s\n", tagToAdd);
|
||||
if (tagToAdd != null)
|
||||
outTag = setTag(key, outTag, tagToAdd);
|
||||
}
|
||||
|
||||
return outTag;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user