Skip to content

Commit

Permalink
Rework to minimize future need for compatibility with other addons.
Browse files Browse the repository at this point in the history
Removed BTHNetServerHandler
  • Loading branch information
jorgebonafe committed May 6, 2018
1 parent c2865f8 commit 5a2d1ef
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 83 deletions.
Binary file removed minecraft/BTHNetServerHandler.class
Binary file not shown.
Binary file added minecraft/CustomBTWAddonNetServerHandler.class
Binary file not shown.
Binary file added minecraft/GenericBTWAddonNetServerHandler.class
Binary file not shown.
8 changes: 8 additions & 0 deletions minecraft/SharedCompatibilityAddonConfig.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This class should contain a list of custom NetServerHandler classes for every BTW addon
// that sends custom network packages. This will ensure compatibility for future releases
// without needing to keep changing every addon's source code when other addons update.
// If a new BTW addon is released that also handles custom network packages this list
// should be updated with it's custom network handler class name, following the same pattern bellow.

class_name=JBNetServerHandler
class_name=AddonNetServerHandler
Binary file modified minecraft/jk.class
Binary file not shown.
Binary file modified minecraft_server/Addon_HayBale$BlockBale.class
Binary file not shown.
Binary file removed minecraft_server/BTHNetServerHandler.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions minecraft_server/SharedCompatibilityAddonConfig.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This class should contain a list of custom NetServerHandler classes for every BTW addon
// that sends custom network packages. This will ensure compatibility for future releases
// without needing to keep changing every addon's source code when other addons update.
// If a new BTW addon is released that also handles custom network packages this list
// should be updated with it's custom network handler class name, following the same pattern bellow.

class_name=JBNetServerHandler
class_name=AddonNetServerHandler
Binary file modified minecraft_server/jk.class
Binary file not shown.
40 changes: 0 additions & 40 deletions src/minecraft/net/minecraft/src/BTHNetServerHandler.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.minecraft.src;

import net.minecraft.server.MinecraftServer;

public abstract class CustomBTWAddonNetServerHandler {
public CustomBTWAddonNetServerHandler(MinecraftServer aServer, INetworkManager aNetManager, EntityPlayerMP aPlayer) {}

public void handleCustomPayload(Packet250CustomPayload aPacket) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package net.minecraft.src;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.util.ArrayList;

import net.minecraft.server.MinecraftServer;

public class GenericBTWAddonNetServerHandler extends NetServerHandler {
private ArrayList<CustomBTWAddonNetServerHandler> customHandlers = new ArrayList<CustomBTWAddonNetServerHandler>();
public final MinecraftServer mcServer;
public EntityPlayerMP playerEntity;

public GenericBTWAddonNetServerHandler(MinecraftServer aServer, INetworkManager aNetManager, EntityPlayerMP aPlayer) {
super(aServer, aNetManager, aPlayer);

this.mcServer = aServer;
this.playerEntity = aPlayer;

String prefix = getClassPackagePrefix();
ArrayList<String> classNames = getCustomNetworkListenersClassNames();

try {
for (String className : classNames) {
Constructor<?> c = Class.forName(prefix + className).getConstructor(MinecraftServer.class, INetworkManager.class,
EntityPlayerMP.class);

CustomBTWAddonNetServerHandler nsh = (CustomBTWAddonNetServerHandler) c.newInstance(aServer, aNetManager, aPlayer);

customHandlers.add(nsh);
}
} catch (Throwable e) {}
}

public void handleCustomPayload(Packet250CustomPayload aPacket) {
for (CustomBTWAddonNetServerHandler netHandler : customHandlers) {
netHandler.handleCustomPayload(aPacket);
}
}

private static ArrayList<String> getCustomNetworkListenersClassNames() {
ArrayList<String> result = new ArrayList<String>();
BufferedReader buffer = null;

try {
buffer = new BufferedReader(
new InputStreamReader(NetworkListenThread.class.getResourceAsStream("/SharedCompatibilityAddonConfig.txt")));
} catch (Throwable e) {
// Currently known addons
result.add("JBNetServerHandler");
result.add("AddonNetServerHandler");
return result;
}

try {
String line = "";

while ((line = buffer.readLine()) != null) {
String[] value = line.split("=");

for (int i = 0; i < value.length; ++i) {
value[i] = value[i].trim();
}

if (!value[0].equals("class_name")) {
continue;
}

result.add(value[1]);
}

buffer.close();
} catch (Throwable e) {
System.out.println("Failed to load SharedCompatibilityAddonConfig.txt");
e.printStackTrace();
}

return result;
}

private String getClassPackagePrefix() {
try {
if (Class.forName("FCBetterThanWolves") != null) {
return "";
}
} catch (Throwable e) {
}
try {
if (Class.forName("net.minecraft.src.FCBetterThanWolves") != null) {
return "net.minecraft.src.";
}
} catch (Throwable e) {
}
return "";
}
}
2 changes: 1 addition & 1 deletion src/minecraft/net/minecraft/src/NetworkListenThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void addPlayer(NetServerHandler par1NetServerHandler)
{
//DNCode Start
//this.connections.add(par1NetServerHandler);
this.connections.add(new BTHNetServerHandler(this.getServer(), par1NetServerHandler.netManager, par1NetServerHandler.playerEntity));
this.connections.add(new GenericBTWAddonNetServerHandler(this.getServer(), par1NetServerHandler.netManager, par1NetServerHandler.playerEntity));
//DNCode End
}

Expand Down
40 changes: 0 additions & 40 deletions src/minecraft_server/net/minecraft/src/BTHNetServerHandler.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.minecraft.src;

import net.minecraft.server.MinecraftServer;

public abstract class CustomBTWAddonNetServerHandler {
public CustomBTWAddonNetServerHandler(MinecraftServer aServer, INetworkManager aNetManager, EntityPlayerMP aPlayer) {}

public void handleCustomPayload(Packet250CustomPayload aPacket) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package net.minecraft.src;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.util.ArrayList;

import net.minecraft.server.MinecraftServer;

public class GenericBTWAddonNetServerHandler extends NetServerHandler {
private ArrayList<CustomBTWAddonNetServerHandler> customHandlers = new ArrayList<CustomBTWAddonNetServerHandler>();
public final MinecraftServer mcServer;
public EntityPlayerMP playerEntity;

public GenericBTWAddonNetServerHandler(MinecraftServer aServer, INetworkManager aNetManager, EntityPlayerMP aPlayer) {
super(aServer, aNetManager, aPlayer);

this.mcServer = aServer;
this.playerEntity = aPlayer;

String prefix = getClassPackagePrefix();
ArrayList<String> classNames = getCustomNetworkListenersClassNames();

try {
for (String className : classNames) {
Constructor<?> c = Class.forName(prefix + className).getConstructor(MinecraftServer.class, INetworkManager.class,
EntityPlayerMP.class);

CustomBTWAddonNetServerHandler nsh = (CustomBTWAddonNetServerHandler) c.newInstance(aServer, aNetManager, aPlayer);

customHandlers.add(nsh);
}
} catch (Throwable e) {}
}

public void handleCustomPayload(Packet250CustomPayload aPacket) {
for (CustomBTWAddonNetServerHandler netHandler : customHandlers) {
netHandler.handleCustomPayload(aPacket);
}
}

private static ArrayList<String> getCustomNetworkListenersClassNames() {
ArrayList<String> result = new ArrayList<String>();
BufferedReader buffer = null;

try {
buffer = new BufferedReader(
new InputStreamReader(NetworkListenThread.class.getResourceAsStream("/SharedCompatibilityAddonConfig.txt")));
} catch (Throwable e) {
// Currently known addons
result.add("JBNetServerHandler");
result.add("AddonNetServerHandler");
return result;
}

try {
String line = "";

while ((line = buffer.readLine()) != null) {
String[] value = line.split("=");

for (int i = 0; i < value.length; ++i) {
value[i] = value[i].trim();
}

if (!value[0].equals("class_name")) {
continue;
}

result.add(value[1]);
}

buffer.close();
} catch (Throwable e) {
System.out.println("Failed to load SharedCompatibilityAddonConfig.txt");
e.printStackTrace();
}

return result;
}

private String getClassPackagePrefix() {
try {
if (Class.forName("FCBetterThanWolves") != null) {
return "";
}
} catch (Throwable e) {
}
try {
if (Class.forName("net.minecraft.src.FCBetterThanWolves") != null) {
return "net.minecraft.src.";
}
} catch (Throwable e) {
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void addPlayer(NetServerHandler par1NetServerHandler)
{
//DNCode Start
//this.connections.add(par1NetServerHandler);
this.connections.add(new BTHNetServerHandler(this.getServer(), par1NetServerHandler.netManager, par1NetServerHandler.playerEntity));
this.connections.add(new GenericBTWAddonNetServerHandler(this.getServer(), par1NetServerHandler.netManager, par1NetServerHandler.playerEntity));
//DNCode End
}

Expand Down Expand Up @@ -75,4 +75,4 @@ public MinecraftServer getServer()
{
return this.mcServer;
}
}
}

0 comments on commit 5a2d1ef

Please sign in to comment.