1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* spotless:off
* KubaTech - Gregtech Addon
* Copyright (C) 2022 - 2024 kuba6000
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
* spotless:on
*/
package kubatech.network;
import java.nio.charset.StandardCharsets;
import net.minecraft.client.Minecraft;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import kubatech.api.tileentity.CustomTileEntityPacketHandler;
import kubatech.api.utils.ModUtils;
import kubatech.kubatech;
public class CustomTileEntityPacket implements IMessage {
public int w, x, y, z;
public final ByteBuf customdata = Unpooled.buffer();
@SuppressWarnings("unused")
public CustomTileEntityPacket() {}
public CustomTileEntityPacket(TileEntity te, byte[] customdata) {
this.w = te.getWorldObj().provider.dimensionId;
this.x = te.xCoord;
this.y = te.yCoord;
this.z = te.zCoord;
if (customdata != null && customdata.length > 0) this.customdata.writeBytes(customdata);
}
public void sendToAllAround(int range) {
kubatech.NETWORK.sendToAllAround(this, new NetworkRegistry.TargetPoint(w, x, y, z, range));
}
// Helper methods
public void resetHelperData() {
customdata.clear();
}
public void addData(byte[] data) {
customdata.writeBytes(data);
}
public void addData(byte data) {
customdata.writeByte(data);
}
public void addData(int data) {
customdata.writeInt(data);
}
public void addData(String data) {
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
addData(bytes.length);
addData(bytes);
}
public void addData(boolean data) {
customdata.writeBoolean(data);
}
public void getData(byte[] bytes) {
customdata.readBytes(bytes);
}
public byte[] getData(int len) {
byte[] bytes = new byte[len];
getData(bytes);
return bytes;
}
public int getDataInt() {
return customdata.readInt();
}
public String getDataString() {
return new String(getData(getDataInt()), StandardCharsets.UTF_8);
}
public boolean getDataBoolean() {
return customdata.readBoolean();
}
@Override
public void fromBytes(ByteBuf buf) {
w = buf.readInt();
x = buf.readInt();
y = buf.readInt();
z = buf.readInt();
customdata.clear();
buf.readBytes(customdata, buf.readInt());
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(w);
buf.writeInt(x);
buf.writeInt(y);
buf.writeInt(z);
buf.writeInt(customdata.readableBytes());
buf.writeBytes(customdata);
}
public static class Handler implements IMessageHandler<CustomTileEntityPacket, IMessage> {
@Override
public IMessage onMessage(CustomTileEntityPacket message, MessageContext ctx) {
if (!ModUtils.isClientThreaded()) return null;
Minecraft mc = Minecraft.getMinecraft();
if (mc == null) return null;
if (mc.thePlayer == null) return null;
World w = mc.thePlayer.getEntityWorld();
if (w == null) return null;
if (message.w != w.provider.dimensionId) return null;
TileEntity e = w.getTileEntity(message.x, message.y, message.z);
if (e == null || e.isInvalid()) return null;
if (e instanceof IGregTechTileEntity && !((IGregTechTileEntity) e).isInvalidTileEntity()) {
IMetaTileEntity mte = ((IGregTechTileEntity) e).getMetaTileEntity();
if (mte == null) return null;
if (!(mte instanceof CustomTileEntityPacketHandler)) return null;
((CustomTileEntityPacketHandler) mte).HandleCustomPacket(message);
return null;
} else if (!(e instanceof CustomTileEntityPacketHandler)) return null;
((CustomTileEntityPacketHandler) e).HandleCustomPacket(message);
return null;
}
}
}
|