diff --git a/TODOLIST b/TODOLIST index dcf12e9aa..56d656c5e 100644 --- a/TODOLIST +++ b/TODOLIST @@ -1,7 +1,6 @@ Migrate EntityUtil to be cool Fix 1.9to1.8 Fix 1.9.3to1.9.1/2 -Fix 1.9.1/2to1.9.3/4 Fix snapshot to 1.10 Fix BaseProtocol Fix BossBar to use Generics diff --git a/common/src/main/java/us/myles/ViaVersion/api/Via.java b/common/src/main/java/us/myles/ViaVersion/api/Via.java index 30983d426..3eec7ed1e 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/Via.java +++ b/common/src/main/java/us/myles/ViaVersion/api/Via.java @@ -21,4 +21,9 @@ public class Via { Validate.isTrue(platform != null, "ViaVersion has not loaded the Platform"); return Via.platform.getApi(); } + + public static ViaVersionConfig getConfig() { + Validate.isTrue(platform != null, "ViaVersion has not loaded the Platform"); + return Via.platform.getConf(); + } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/minecraft/Environment.java b/common/src/main/java/us/myles/ViaVersion/api/minecraft/Environment.java new file mode 100644 index 000000000..fb098741a --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/minecraft/Environment.java @@ -0,0 +1,32 @@ +package us.myles.ViaVersion.api.minecraft; + +import java.util.HashMap; +import java.util.Map; + +public enum Environment { + NORMAL(0), + NETHER(-1), + END(1); + + private final int id; + private static final Map lookup = new HashMap<>(); + + static { + for (Environment env : values()) { + lookup.put(env.getId(), env); + } + } + + Environment(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + + public static Environment getEnvironmentById(int id) { + return lookup.get(id); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java index 599376e81..685fcdda9 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java @@ -1,21 +1,21 @@ package us.myles.ViaVersion.protocols.base; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import io.netty.channel.ChannelFuture; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; +import net.md_5.bungee.api.ChatColor; import org.apache.commons.lang.StringUtils; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Pair; -import us.myles.ViaVersion.api.ViaVersion; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.Protocol; import us.myles.ViaVersion.api.protocol.ProtocolPipeline; @@ -32,6 +32,7 @@ import java.util.UUID; import java.util.logging.Level; public class BaseProtocol extends Protocol { + private static Gson gson = new GsonBuilder().create(); // TODO: Possibly global gson provider? @Override protected void registerPackets() { @@ -48,30 +49,32 @@ public class BaseProtocol extends Protocol { ProtocolInfo info = wrapper.user().get(ProtocolInfo.class); String originalStatus = wrapper.get(Type.STRING, 0); try { - JSONObject json = (JSONObject) new JSONParser().parse(originalStatus); - JSONObject version = (JSONObject) json.get("version"); - int protocolVersion = ((Long) version.get("protocol")).intValue(); + JsonObject json = gson.fromJson(originalStatus, JsonObject.class); + JsonObject version = json.get("version").getAsJsonObject(); + int protocolVersion = ((Long) version.get("protocol").getAsLong()).intValue(); - if (ViaVersion.getConfig().isSendSupportedVersions()) //Send supported versions - version.put("supportedVersions", ViaVersion.getInstance().getSupportedVersions()); + if (Via.getConfig().isSendSupportedVersions()) //Send supported versions + version.add("supportedVersions", gson.toJsonTree(Via.getAPI().getSupportedVersions())); if (ProtocolRegistry.SERVER_PROTOCOL == -1) // Set the Server protocol if the detection on startup failed ProtocolRegistry.SERVER_PROTOCOL = protocolVersion; List> protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), ProtocolRegistry.SERVER_PROTOCOL); if (protocols != null) { - if (protocolVersion != 9999) //Fix ServerListPlus - version.put("protocol", info.getProtocolVersion()); + if (protocolVersion != 9999) { + //Fix ServerListPlus + version.addProperty("protocol", info.getProtocolVersion()); + } } else { // not compatible :(, *plays very sad violin* wrapper.user().setActive(false); } - if (ViaVersion.getConfig().getBlockedProtocols().contains(info.getProtocolVersion())) - version.put("protocol", -1); // Show blocked versions as outdated + if (Via.getConfig().getBlockedProtocols().contains(info.getProtocolVersion())) + version.addProperty("protocol", -1); // Show blocked versions as outdated - wrapper.set(Type.STRING, 0, json.toJSONString()); // Update value - } catch (ParseException e) { + wrapper.set(Type.STRING, 0, gson.toJson(json)); // Update value + } catch (JsonParseException e) { e.printStackTrace(); } } @@ -100,14 +103,14 @@ public class BaseProtocol extends Protocol { info.setUuid(uuid); info.setUsername(wrapper.get(Type.STRING, 1)); // Add to ported clients - ((ViaVersionPlugin) ViaVersion.getInstance()).addPortedClient(wrapper.user()); + Via.getManager().addPortedClient(wrapper.user()); if (info.getPipeline().pipes().size() == 1 && info.getPipeline().pipes().get(0).getClass() == BaseProtocol.class) // Only base protocol wrapper.user().setActive(false); - if (ViaVersion.getInstance().isDebug()) { + if (Via.getManager().isDebug()) { // Print out the route to console - ((ViaVersionPlugin) ViaVersion.getInstance()).getLogger().log(Level.INFO, "{0} logged in with protocol {1}, Route: {2}", + Via.getPlatform().getLogger().log(Level.INFO, "{0} logged in with protocol {1}, Route: {2}", new Object[]{ wrapper.get(Type.STRING, 1), info.getProtocolVersion(), @@ -171,11 +174,11 @@ public class BaseProtocol extends Protocol { @Override public void handle(final PacketWrapper wrapper) throws Exception { int protocol = wrapper.user().get(ProtocolInfo.class).getProtocolVersion(); - if (ViaVersion.getConfig().getBlockedProtocols().contains(protocol)) { + if (Via.getConfig().getBlockedProtocols().contains(protocol)) { if (!wrapper.user().getChannel().isOpen()) return; PacketWrapper disconnectPacket = new PacketWrapper(0x00, null, wrapper.user()); // Disconnect Packet - Protocol1_9TO1_8.FIX_JSON.write(disconnectPacket, ChatColor.translateAlternateColorCodes('&', ViaVersion.getConfig().getBlockedDisconnectMsg())); + Protocol1_9TO1_8.FIX_JSON.write(disconnectPacket, ChatColor.translateAlternateColorCodes('&', Via.getConfig().getBlockedDisconnectMsg())); wrapper.cancel(); // cancel current // Send and close diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/chunks/BlockEntity.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/chunks/BlockEntity.java index 96ca9f985..bda4b1731 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/chunks/BlockEntity.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/chunks/BlockEntity.java @@ -3,7 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.chunks; import lombok.Getter; import org.spacehq.opennbt.tag.builtin.CompoundTag; import us.myles.ViaVersion.api.PacketWrapper; -import us.myles.ViaVersion.api.ViaVersion; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.type.Type; @@ -51,7 +51,7 @@ public class BlockEntity { updateBlockEntity(pos, (short) newId, tag, connection); } catch (Exception e) { - if (ViaVersion.getInstance().isDebug()) { + if (Via.getManager().isDebug()) { System.out.println("Block Entity: " + e.getMessage() + ": " + tag); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/types/Chunk1_9_3_4Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/types/Chunk1_9_3_4Type.java index 684f1e4a3..c1552692e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/types/Chunk1_9_3_4Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_1_2to1_9_3_4/types/Chunk1_9_3_4Type.java @@ -2,8 +2,8 @@ package us.myles.ViaVersion.protocols.protocol1_9_1_2to1_9_3_4.types; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import org.bukkit.World; import org.spacehq.opennbt.tag.builtin.CompoundTag; +import us.myles.ViaVersion.api.minecraft.Environment; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.type.PartialType; @@ -48,7 +48,7 @@ public class Chunk1_9_3_4Type extends PartialType { sections[i] = section; section.readBlocks(input); section.readBlockLight(input); - if (world.getEnvironment() == World.Environment.NORMAL) { + if (world.getEnvironment() == Environment.NORMAL) { section.readSkyLight(input); } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/storage/ClientWorld.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/storage/ClientWorld.java index 8c67f60eb..9dcca3354 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/storage/ClientWorld.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/storage/ClientWorld.java @@ -1,13 +1,13 @@ package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage; import lombok.Getter; -import org.bukkit.World; import us.myles.ViaVersion.api.data.StoredObject; import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.Environment; @Getter public class ClientWorld extends StoredObject { - private World.Environment environment; + private Environment environment; public ClientWorld(UserConnection user) { super(user); @@ -17,17 +17,9 @@ public class ClientWorld extends StoredObject { this.environment = getEnvFromId(environmentId); } - private World.Environment getEnvFromId(int id) { - switch(id) { - case -1: - return World.Environment.NETHER; - case 0: - return World.Environment.NORMAL; - case 1: - return World.Environment.THE_END; - default: - return World.Environment.NETHER; - //throw new IllegalArgumentException("Invalid environment id:" + id); - } + private Environment getEnvFromId(int id) { + Environment output = Environment.getEnvironmentById(id); + if (output == null) return Environment.NETHER; + return output; } } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java index cb5d31c10..01dcd75db 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9_3to1_9_1_2/types/Chunk1_9_1_2Type.java @@ -2,9 +2,9 @@ package us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.types; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import org.bukkit.World; import org.spacehq.opennbt.tag.builtin.CompoundTag; -import us.myles.ViaVersion.api.ViaVersion; +import us.myles.ViaVersion.api.Via; +import us.myles.ViaVersion.api.minecraft.Environment; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; import us.myles.ViaVersion.api.minecraft.chunks.ChunkSection; import us.myles.ViaVersion.api.type.PartialType; @@ -27,8 +27,8 @@ public class Chunk1_9_1_2Type extends PartialType { @Override public Chunk read(ByteBuf input, ClientWorld world) throws Exception { - boolean replacePistons = world.getUser().get(ProtocolInfo.class).getPipeline().contains(Protocol1_10To1_9_3_4.class) && ViaVersion.getConfig().isReplacePistons(); - int replacementId = ViaVersion.getConfig().getPistonReplacementId(); + boolean replacePistons = world.getUser().get(ProtocolInfo.class).getPipeline().contains(Protocol1_10To1_9_3_4.class) && Via.getConfig().isReplacePistons(); + int replacementId = Via.getConfig().getPistonReplacementId(); int chunkX = input.readInt(); int chunkZ = input.readInt(); @@ -53,7 +53,7 @@ public class Chunk1_9_1_2Type extends PartialType { sections[i] = section; section.readBlocks(input); section.readBlockLight(input); - if (world.getEnvironment() == World.Environment.NORMAL) { + if (world.getEnvironment() == Environment.NORMAL) { section.readSkyLight(input); } if (replacePistons) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter.java index 5783a0cc7..3f45056b6 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/metadata/MetadataRewriter.java @@ -3,6 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_9to1_8.metadata; import org.bukkit.entity.EntityType; import org.bukkit.util.EulerAngle; import org.bukkit.util.Vector; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.minecraft.item.Item; import us.myles.ViaVersion.api.minecraft.metadata.Metadata; @@ -140,7 +141,7 @@ public class MetadataRewriter { } } catch (Exception e) { list.remove(entry); - if (!ViaVersion.getConfig().isSuppressMetadataErrors() || ViaVersion.getInstance().isDebug()) { + if (!Via.getConfig().isSuppressMetadataErrors() || Via.getManager().isDebug()) { System.out.println("INCLUDE THIS IN YOUR ERROR LOG!"); if (type != null) System.out.println("An error occurred with entity meta data for " + type + " OldID: " + entry.getId());