33
44import api .AddonHandler ;
55import api .BTWAddon ;
6+ import api .network .CustomPacketHandler ;
7+ import net .fabricmc .api .EnvType ;
8+ import net .fabricmc .api .Environment ;
69import net .minecraft .server .MinecraftServer ;
710import net .minecraft .src .*;
811
9- import java .io .ByteArrayInputStream ;
10- import java .io .ByteArrayOutputStream ;
11- import java .io .DataInputStream ;
12- import java .io .DataOutputStream ;
12+ import java .io .*;
1313import java .util .List ;
1414
1515public class RPGAddon extends BTWAddon {
@@ -43,19 +43,6 @@ public RPGAddon() {
4343 @ Override
4444 public void initialize () {
4545 AddonHandler .logMessage (this .getName () + " Version " + this .getVersionString () + " Initializing..." );
46- registerPacketHandler ("rpg|StatsC" , (packet , player_ ) -> {
47- // 1.5
48- // Client-Side: You get a packet with the stats of the player
49- DataInputStream data = new DataInputStream (new ByteArrayInputStream (packet .data ));
50- ((RPGStats ) Minecraft .getMinecraft ().thePlayer ).doReinit (new RPGPointsAllocation (data ));
51- });
52- registerPacketHandler ("rpg|Screen" , (packet , player_ ) -> {
53- // Client-Side: You get a packet to open the stats screen
54- EntityPlayer player ;
55- Minecraft .getMinecraft ().displayGuiScreen (new GuiRPGStats (Minecraft .getMinecraft ().currentScreen ,
56- (player = Minecraft .getMinecraft ().thePlayer ) != null ? ((RPGStats ) player ).getAllocation () : RPGPointsAllocation .defaultAllocation (RPGPointsAllocation .DEFAULT_POINTS ),
57- true ));
58- });
5946 AddonHandler .registerCommand (new CommandBase () {
6047 @ Override
6148 public String getCommandName () {
@@ -91,6 +78,16 @@ public List addTabCompletionOptions(ICommandSender par1ICommandSender, String[]
9178 }
9279
9380 }, false );
81+
82+ if (!MinecraftServer .getIsServer ()) {
83+ registerClientPacketHandlers ();
84+ }
85+ }
86+
87+ @ Environment (EnvType .CLIENT )
88+ private void registerClientPacketHandlers () {
89+ registerPacketHandler ("rpg|StatsC" , new ClientStatPacketHandler ());
90+ registerPacketHandler ("rpg|Screen" , new ClientRPGGuiPacketHandler ());
9491 }
9592
9693 public static void sendStatAllocationScreenToPlayer (EntityPlayerMP player ) {
@@ -139,6 +136,7 @@ public static void sendStatAllocationToPlayer(NetServerHandler serverHandler, En
139136 }
140137 }
141138
139+ @ Environment (EnvType .CLIENT )
142140 public static void sendStatAllocationToServer (RPGPointsAllocation pointsAllocation , boolean force ) {
143141 ByteArrayOutputStream byteStream = new ByteArrayOutputStream ();
144142 DataOutputStream dataStream = new DataOutputStream (byteStream );
@@ -154,4 +152,26 @@ public static RPGAddon getInstance() {
154152 return instance ;
155153 }
156154
155+ @ Environment (EnvType .CLIENT )
156+ private static class ClientStatPacketHandler implements CustomPacketHandler {
157+ @ Override
158+ public void handleCustomPacket (Packet250CustomPayload packet , EntityPlayer player_ ) throws IOException {
159+ // 1.5
160+ // Client-Side: You get a packet with the stats of the player
161+ DataInputStream data = new DataInputStream (new ByteArrayInputStream (packet .data ));
162+ ((RPGStats ) Minecraft .getMinecraft ().thePlayer ).doReinit (new RPGPointsAllocation (data ));
163+ }
164+ }
165+
166+ @ Environment (EnvType .CLIENT )
167+ private static class ClientRPGGuiPacketHandler implements CustomPacketHandler {
168+ @ Override
169+ public void handleCustomPacket (Packet250CustomPayload packet , EntityPlayer player_ ) throws IOException {
170+ // Client-Side: You get a packet to open the stats screen
171+ EntityPlayer player ;
172+ Minecraft .getMinecraft ().displayGuiScreen (new GuiRPGStats (Minecraft .getMinecraft ().currentScreen ,
173+ (player = Minecraft .getMinecraft ().thePlayer ) != null ? ((RPGStats ) player ).getAllocation () : RPGPointsAllocation .defaultAllocation (RPGPointsAllocation .DEFAULT_POINTS ),
174+ true ));
175+ }
176+ }
157177}
0 commit comments