mirror of
https://github.com/GTNewHorizons/Opis.git
synced 2026-11-20 04:09:41 +00:00
Strings can now be sent as integers and the cache will be synced between server & client.
80 lines
2.2 KiB
Java
80 lines
2.2 KiB
Java
package mcp.mobius.opis.data.managers;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import com.google.common.collect.HashBiMap;
|
|
|
|
import cpw.mods.fml.common.network.PacketDispatcher;
|
|
import cpw.mods.fml.common.network.Player;
|
|
import mcp.mobius.opis.modOpis;
|
|
import mcp.mobius.opis.api.IMessageHandler;
|
|
import mcp.mobius.opis.data.holders.ISerializable;
|
|
import mcp.mobius.opis.data.holders.newtypes.DataStringUpdate;
|
|
import mcp.mobius.opis.network.enums.Message;
|
|
import mcp.mobius.opis.network.packets.server.NetDataList;
|
|
import mcp.mobius.opis.network.packets.server.NetDataRaw;
|
|
import mcp.mobius.opis.network.packets.server.NetDataValue;
|
|
|
|
public enum StringCache implements IMessageHandler {
|
|
INSTANCE;
|
|
|
|
private int currentIndex = -1;
|
|
private HashBiMap<Integer, String> cache = HashBiMap.create();
|
|
private ArrayList<DataStringUpdate> toSend = new ArrayList<DataStringUpdate>();
|
|
|
|
public String getString(int index){
|
|
String retVal = this.cache.get(index);
|
|
if (retVal == null)
|
|
return "<ERROR>";
|
|
else
|
|
return retVal;
|
|
}
|
|
|
|
public int getIndex(String str){
|
|
if (cache.inverse().containsKey(str)){
|
|
return cache.inverse().get(str);
|
|
}
|
|
else{
|
|
currentIndex += 1;
|
|
cache.put(currentIndex, str);
|
|
|
|
DataStringUpdate upd = new DataStringUpdate(str, currentIndex);
|
|
this.toSend.add(upd);
|
|
|
|
PacketDispatcher.sendPacketToAllPlayers(NetDataValue.create(Message.STATUS_STRINGUPD, upd).packet);
|
|
return currentIndex;
|
|
}
|
|
}
|
|
|
|
public void syncCache(Player player){
|
|
// NEED TO AUTO SPLIT THE PACKET !
|
|
PacketDispatcher.sendPacketToPlayer(NetDataList.create(Message.STATUS_STRINGUPD_FULL, this.toSend).packet, player);
|
|
}
|
|
|
|
@Override
|
|
public boolean handleMessage(Message msg, NetDataRaw rawdata) {
|
|
switch(msg){
|
|
case STATUS_STRINGUPD:{
|
|
modOpis.log.info("Received String Cache update");
|
|
DataStringUpdate data = (DataStringUpdate)rawdata.value;
|
|
this.cache.put(data.index, data.str);
|
|
break;
|
|
}
|
|
case STATUS_STRINGUPD_FULL:{
|
|
modOpis.log.info(String.format("Received full String Cache update containing %d entries", rawdata.array.size()));
|
|
for (ISerializable idata : rawdata.array){
|
|
DataStringUpdate data = (DataStringUpdate)idata;
|
|
this.cache.put(data.index, data.str);
|
|
}
|
|
|
|
break;
|
|
}
|
|
default:
|
|
return false;
|
|
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|