mirror of
https://github.com/GTNewHorizons/Opis.git
synced 2026-11-17 11:05:05 +00:00
Upon clicking on a chunk in MeanTime overlay, the client receive a list of entities for this chunk.
Added a ISerializable interface to make it easier to send/receive complex packets.
This commit is contained in:
1 parent
f9fb81aa26
commit
f49abc3ecb
10 files changed
+186
-22
No files matched your search
@@ -7,7 +7,7 @@ public class ChunkStatsData {
|
||||
public ChunkStatsData(){}
|
||||
|
||||
public ChunkStatsData(int nentities, double updateTime){
|
||||
this.nentities = nentities;
|
||||
this.nentities = nentities;
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
package mcp.mobius.opis.data;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
public final class CoordinatesBlock {
|
||||
public final class CoordinatesBlock implements ISerializable {
|
||||
public final int dim, x, y, z;
|
||||
public final int chunkX, chunkZ;
|
||||
//public boolean isChunk;
|
||||
@@ -69,5 +73,19 @@ public final class CoordinatesBlock {
|
||||
// return String.format("%s %s %s", this.dim, this.chunkX, this.chunkZ).hashCode();
|
||||
//else
|
||||
return String.format("%s %s %s %s", this.dim, this.x, this.y, this.z).hashCode();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(DataOutputStream stream) throws IOException {
|
||||
stream.writeInt(this.dim);
|
||||
stream.writeInt(this.x);
|
||||
stream.writeInt(this.y);
|
||||
stream.writeInt(this.z);
|
||||
}
|
||||
|
||||
public static CoordinatesBlock readFromStream(DataInputStream stream) throws IOException {
|
||||
return new CoordinatesBlock(stream.readInt(), stream.readInt(), stream.readInt(), stream.readInt());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
package mcp.mobius.opis.data;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
public final class CoordinatesChunk {
|
||||
public final class CoordinatesChunk implements ISerializable {
|
||||
public final int dim, x, y, z;
|
||||
public final int chunkX, chunkZ;
|
||||
//public boolean isChunk;
|
||||
@@ -48,5 +52,16 @@ public final class CoordinatesChunk {
|
||||
|
||||
public int hashCode() {
|
||||
return String.format("%s %s %s", this.dim, this.chunkX, this.chunkZ).hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(DataOutputStream stream) throws IOException {
|
||||
stream.writeInt(this.dim);
|
||||
stream.writeInt(this.chunkX);
|
||||
stream.writeInt(this.chunkZ);
|
||||
}
|
||||
|
||||
public static CoordinatesChunk readFromStream(DataInputStream stream) throws IOException {
|
||||
return new CoordinatesChunk(stream.readInt(), stream.readInt(), stream.readInt());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package mcp.mobius.opis.data;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface ISerializable {
|
||||
void writeToStream(DataOutputStream stream) throws IOException;
|
||||
//Object readFromStream(DataInputStream stream) throws IOException;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package mcp.mobius.opis.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
@@ -17,14 +18,13 @@ public class TileEntitiesData {
|
||||
|
||||
if (!(references.containsKey(coord))){
|
||||
references.put(coord, te.getClass());
|
||||
stats.put(coord, new TileEntityStatsData());
|
||||
stats.put(coord, new TileEntityStatsData(coord, te.getClass().getName()));
|
||||
}
|
||||
|
||||
stats.get(coord).addMeasure(timing);
|
||||
}
|
||||
|
||||
public static HashMap<CoordinatesChunk, ChunkStatsData> getTimes(int dim){
|
||||
|
||||
HashMap<CoordinatesChunk, ChunkStatsData> chunks = new HashMap<CoordinatesChunk, ChunkStatsData>();
|
||||
|
||||
for (CoordinatesBlock coord : TileEntitiesData.stats.keySet()){
|
||||
@@ -36,12 +36,19 @@ public class TileEntitiesData {
|
||||
|
||||
chunks.get(coordC).nentities += 1;
|
||||
chunks.get(coordC).updateTime += stats.get(coord).getGeometricMean();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
|
||||
public static ArrayList<TileEntityStatsData> getInChunk(CoordinatesChunk coord){
|
||||
ArrayList<TileEntityStatsData> returnList = new ArrayList<TileEntityStatsData>();
|
||||
|
||||
for (CoordinatesBlock tecoord : TileEntitiesData.stats.keySet()){
|
||||
if (coord.equals(new CoordinatesChunk(tecoord)))
|
||||
returnList.add(TileEntitiesData.stats.get(tecoord));
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,65 @@
|
||||
package mcp.mobius.opis.data;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.network.packet.Packet;
|
||||
|
||||
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
|
||||
|
||||
public class TileEntityStatsData {
|
||||
public class TileEntityStatsData implements ISerializable {
|
||||
|
||||
private DescriptiveStatistics stats = new DescriptiveStatistics(250);
|
||||
private CoordinatesBlock coord;
|
||||
private String classname;
|
||||
private Double geomMean = null;
|
||||
|
||||
|
||||
public TileEntityStatsData(CoordinatesBlock coord, String teclass){
|
||||
this.coord = coord;
|
||||
this.classname = teclass;
|
||||
}
|
||||
|
||||
public void addMeasure(long timing){
|
||||
stats.addValue(timing/1000.0);
|
||||
}
|
||||
|
||||
public double getGeometricMean(){
|
||||
return stats.getGeometricMean();
|
||||
if (geomMean != null)
|
||||
return geomMean;
|
||||
else
|
||||
return stats.getGeometricMean();
|
||||
}
|
||||
|
||||
public void setGeometricMean(double value){
|
||||
this.geomMean = value;
|
||||
}
|
||||
|
||||
public CoordinatesBlock getCoordinates(){
|
||||
return this.coord;
|
||||
}
|
||||
|
||||
public CoordinatesChunk getChunk(){
|
||||
return new CoordinatesChunk(this.coord);
|
||||
}
|
||||
|
||||
public String getType(){
|
||||
return this.classname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToStream(DataOutputStream stream) throws IOException{
|
||||
this.coord.writeToStream(stream);
|
||||
Packet.writeString(this.classname, stream);
|
||||
stream.writeDouble(this.getGeometricMean());
|
||||
}
|
||||
|
||||
public static TileEntityStatsData readFromStream(DataInputStream stream) throws IOException {
|
||||
CoordinatesBlock coord = CoordinatesBlock.readFromStream(stream);
|
||||
String classname = Packet.readString(stream, 255);
|
||||
TileEntityStatsData stat = new TileEntityStatsData(coord, classname);
|
||||
stat.setGeometricMean(stream.readDouble());
|
||||
return stat;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import java.util.logging.Level;
|
||||
import mcp.mobius.opis.modOpis;
|
||||
import mcp.mobius.opis.data.ChunksData;
|
||||
import mcp.mobius.opis.data.TileEntitiesData;
|
||||
import mcp.mobius.opis.data.TileEntityStatsData;
|
||||
import mcp.mobius.opis.overlay.OverlayLoadedChunks;
|
||||
import mcp.mobius.opis.overlay.OverlayStatus;
|
||||
import net.minecraft.network.INetworkManager;
|
||||
@@ -27,10 +28,30 @@ public class OpisPacketHandler implements IPacketHandler {
|
||||
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
|
||||
if (packet.channel.equals("Opis")) {
|
||||
byte header = this.getHeader(packet);
|
||||
|
||||
// CLIENT RECEIVED PACKETS
|
||||
if (header == Packets.LOADED_CHUNKS){
|
||||
Packet_LoadedChunks castedPacket = new Packet_LoadedChunks(packet);
|
||||
ChunksData.chunksLoad = castedPacket.chunkStatus;
|
||||
}
|
||||
|
||||
else if (header == Packets.MEANTIME){
|
||||
Packet_MeanTime castedPacket = new Packet_MeanTime(packet);
|
||||
ChunksData.chunkMeanTime = castedPacket.chunkStatus;
|
||||
}
|
||||
|
||||
else if (header == Packets.TILEENTITIES_LIST){
|
||||
Packet_TileEntitiesList castedPacket = new Packet_TileEntitiesList(packet);
|
||||
for (TileEntityStatsData data : castedPacket.entities)
|
||||
System.out.printf("%s %.5f ms\n", data.getType(), data.getGeometricMean() / 1000.0);
|
||||
}
|
||||
|
||||
// SERVER RECEIVED PACKETS
|
||||
else if (header == Packets.UNREGISTER_USER){
|
||||
Packet_UnregisterPlayer castedPacket = new Packet_UnregisterPlayer(packet);
|
||||
modOpis.proxy.playerOverlayStatus.remove(player);
|
||||
modOpis.proxy.playerDimension.remove(player);
|
||||
}
|
||||
|
||||
else if (header == Packets.REQ_CHUNKS_IN_DIM){
|
||||
Packet_ReqChunksInDim castedPacket = new Packet_ReqChunksInDim(packet);
|
||||
@@ -39,12 +60,6 @@ public class OpisPacketHandler implements IPacketHandler {
|
||||
PacketDispatcher.sendPacketToPlayer(Packet_LoadedChunks.create(ChunksData.getLoadedChunks(castedPacket.dimension)), player);
|
||||
}
|
||||
|
||||
else if (header == Packets.UNREGISTER_USER){
|
||||
Packet_UnregisterPlayer castedPacket = new Packet_UnregisterPlayer(packet);
|
||||
modOpis.proxy.playerOverlayStatus.remove(player);
|
||||
modOpis.proxy.playerDimension.remove(player);
|
||||
}
|
||||
|
||||
else if (header == Packets.REQ_CHUNKS_ALL){
|
||||
Packet_ReqChunksAll castedPacket = new Packet_ReqChunksAll(packet);
|
||||
}
|
||||
@@ -56,13 +71,9 @@ public class OpisPacketHandler implements IPacketHandler {
|
||||
PacketDispatcher.sendPacketToPlayer(Packet_MeanTime.create(TileEntitiesData.getTimes(castedPacket.dimension), castedPacket.dimension), player);
|
||||
}
|
||||
|
||||
else if (header == Packets.MEANTIME){
|
||||
Packet_MeanTime castedPacket = new Packet_MeanTime(packet);
|
||||
ChunksData.chunkMeanTime = castedPacket.chunkStatus;
|
||||
}
|
||||
|
||||
else if (header == Packets.REQ_TES_IN_CHUNK){
|
||||
Packet_ReqTEsInChunk castedPacket = new Packet_ReqTEsInChunk(packet);
|
||||
PacketDispatcher.sendPacketToPlayer(Packet_TileEntitiesList.create(TileEntitiesData.getInChunk(castedPacket.chunk)), player);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package mcp.mobius.opis.network;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import mcp.mobius.opis.data.TileEntityStatsData;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
|
||||
public class Packet_TileEntitiesList {
|
||||
|
||||
public byte header;
|
||||
ArrayList<TileEntityStatsData> entities = new ArrayList<TileEntityStatsData>();
|
||||
|
||||
public Packet_TileEntitiesList(Packet250CustomPayload packet) {
|
||||
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
|
||||
|
||||
try{
|
||||
this.header = inputStream.readByte();
|
||||
int ndata = inputStream.readInt();
|
||||
for (int i = 0; i < ndata; i++)
|
||||
entities.add(TileEntityStatsData.readFromStream(inputStream));
|
||||
} catch (IOException e){}
|
||||
}
|
||||
|
||||
public static Packet250CustomPayload create(ArrayList<TileEntityStatsData> stats){
|
||||
Packet250CustomPayload packet = new Packet250CustomPayload();
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(1);
|
||||
DataOutputStream outputStream = new DataOutputStream(bos);
|
||||
|
||||
try{
|
||||
outputStream.writeByte(Packets.TILEENTITIES_LIST);
|
||||
outputStream.writeInt(stats.size());
|
||||
for (TileEntityStatsData data : stats)
|
||||
data.writeToStream(outputStream);
|
||||
}catch(IOException e){}
|
||||
|
||||
packet.channel = "Opis";
|
||||
packet.data = bos.toByteArray();
|
||||
packet.length = bos.size();
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@ public class Packets {
|
||||
public static byte REQ_MEANTIME_ALL = 0x12; // C => S //Not used right now
|
||||
|
||||
public static byte REQ_TES_IN_CHUNK = 0x20; // C => S
|
||||
public static byte TILEENTITIES_LIST = 0x21; // S => C
|
||||
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import mapwriter.map.MapView;
|
||||
import mcp.mobius.opis.data.ChunksData;
|
||||
import mcp.mobius.opis.data.CoordinatesChunk;
|
||||
import mcp.mobius.opis.network.Packet_ReqMeanTimeInDim;
|
||||
import mcp.mobius.opis.network.Packet_ReqTEsInChunk;
|
||||
import mcp.mobius.opis.network.Packet_UnregisterPlayer;
|
||||
|
||||
public class OverlayMeanTime implements IMwDataProvider {
|
||||
@@ -112,6 +113,10 @@ public class OverlayMeanTime implements IMwDataProvider {
|
||||
} else {
|
||||
this.selectedChunk = null;
|
||||
}
|
||||
|
||||
if (this.selectedChunk != null)
|
||||
PacketDispatcher.sendPacketToServer(Packet_ReqTEsInChunk.create(this.selectedChunk));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user