From 3134e972a5e52bd7747ebbb6705bccc3f497c413 Mon Sep 17 00:00:00 2001 From: Sekwah Date: Thu, 18 Oct 2018 03:27:10 +0100 Subject: [PATCH] Started work to remake the portal cancelation --- .../AdvancedPortalsPlugin.java | 48 ++----------------- .../advancedportals/compat/CraftBukkit.java | 13 ++--- .../injector/PacketInjector.java | 14 ++++++ .../listeners/PortalPlacer.java | 12 ++--- .../reflection/ReflectionHelper.java | 17 +++++++ src/main/resources/config.yml | 4 +- 6 files changed, 48 insertions(+), 60 deletions(-) create mode 100644 src/main/java/com/sekwah/advancedportals/injector/PacketInjector.java create mode 100644 src/main/java/com/sekwah/advancedportals/reflection/ReflectionHelper.java diff --git a/src/main/java/com/sekwah/advancedportals/AdvancedPortalsPlugin.java b/src/main/java/com/sekwah/advancedportals/AdvancedPortalsPlugin.java index 7ba6b64..6342b97 100644 --- a/src/main/java/com/sekwah/advancedportals/AdvancedPortalsPlugin.java +++ b/src/main/java/com/sekwah/advancedportals/AdvancedPortalsPlugin.java @@ -4,6 +4,7 @@ import com.sekwah.advancedportals.compat.CraftBukkit; import com.sekwah.advancedportals.destinations.Destination; import com.sekwah.advancedportals.destinations.DestinationCommand; import com.sekwah.advancedportals.effects.WarpEffects; +import com.sekwah.advancedportals.injector.PacketInjector; import com.sekwah.advancedportals.listeners.*; import com.sekwah.advancedportals.metrics.Metrics; import com.sekwah.advancedportals.portals.Portal; @@ -36,53 +37,12 @@ public class AdvancedPortalsPlugin extends JavaPlugin { this.compat = new CraftBukkit(this, version); + ConfigAccessor config = new ConfigAccessor(this, "config.yml"); - // This is a fix for my stupidity with the last version - File dataFolder = this.getDataFolder(); - File configFile = new File(dataFolder, "portals.yml"); - - InputStreamReader inr = null; - try { - inr = new InputStreamReader(new FileInputStream(configFile), "ASCII"); - BufferedReader br = new BufferedReader(inr); - StringBuffer sb = new StringBuffer(); - while (true) { - String line = br.readLine(); - if (line == null) - break; - sb.append(line); - sb.append("\n"); - } - br.close(); - inr.close(); - - String fileContents = sb.toString(); - - fileContents = fileContents.replaceAll(" getPos1\\(\\):", " pos1:"); - fileContents = fileContents.replaceAll(" getPos2\\(\\):", " pos2:"); - fileContents = fileContents.replaceAll(" getBungee\\(\\):", " bungee:"); - - try { - FileWriter fileWriter = new FileWriter(configFile); - - BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); - bufferedWriter.write(fileContents); - bufferedWriter.flush(); - bufferedWriter.close(); - fileWriter.close(); - } - catch(IOException e) { - e.printStackTrace(); - } - } catch (UnsupportedEncodingException e) { - this.getLogger().warning("Invalid Encoding"); - } catch (FileNotFoundException e) { - this.getLogger().info("File not found"); - } catch (IOException e) { - e.printStackTrace(); + if(config.getConfig().getBoolean("DisableGatewayBeam", true)) { + new PacketInjector(this, version); } - ConfigAccessor portalConfig = new ConfigAccessor(this, "portals.yml"); portalConfig.saveDefaultConfig(); diff --git a/src/main/java/com/sekwah/advancedportals/compat/CraftBukkit.java b/src/main/java/com/sekwah/advancedportals/compat/CraftBukkit.java index 64fba32..19134f6 100644 --- a/src/main/java/com/sekwah/advancedportals/compat/CraftBukkit.java +++ b/src/main/java/com/sekwah/advancedportals/compat/CraftBukkit.java @@ -1,6 +1,7 @@ package com.sekwah.advancedportals.compat; import com.sekwah.advancedportals.AdvancedPortalsPlugin; +import com.sekwah.advancedportals.reflection.ReflectionHelper; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -81,15 +82,9 @@ public class CraftBukkit { getTileEntityMethod = Class.forName(minecraftPackage + "WorldServer").getMethod("getTileEntity", blockPos); - Field[] endGatewayFields = Class.forName(minecraftPackage + "TileEntityEndGateway").getDeclaredFields(); - - for(Field field : endGatewayFields) { - if(field.getType() == int.class && !field.isAccessible()) { - field.setAccessible(true); - getEntityTimeoutField = field; - this.plugin.getLogger().info("Got field " + field.getName() + " from TileEntityEndGateway"); - return; - } + getEntityTimeoutField = ReflectionHelper.getFieldByType(Class.forName(minecraftPackage + "TileEntityEndGateway"), int.class, false); + if(getEntityTimeoutField != null) { + this.plugin.getLogger().info("Got field " + getEntityTimeoutField.getName() + " from TileEntityEndGateway"); } } catch (Exception e) { diff --git a/src/main/java/com/sekwah/advancedportals/injector/PacketInjector.java b/src/main/java/com/sekwah/advancedportals/injector/PacketInjector.java new file mode 100644 index 0000000..cd74bcb --- /dev/null +++ b/src/main/java/com/sekwah/advancedportals/injector/PacketInjector.java @@ -0,0 +1,14 @@ +package com.sekwah.advancedportals.injector; + +import com.sekwah.advancedportals.AdvancedPortalsPlugin; + +public class PacketInjector { + + public PacketInjector(AdvancedPortalsPlugin plugin, String bukkitImpl) { + String craftBukkitPackage = "org.bukkit.craftbukkit." + bukkitImpl + "."; + String minecraftPackage = "net.minecraft.server." + bukkitImpl + "."; + + + } + +} diff --git a/src/main/java/com/sekwah/advancedportals/listeners/PortalPlacer.java b/src/main/java/com/sekwah/advancedportals/listeners/PortalPlacer.java index a4d6021..52ec6a8 100644 --- a/src/main/java/com/sekwah/advancedportals/listeners/PortalPlacer.java +++ b/src/main/java/com/sekwah/advancedportals/listeners/PortalPlacer.java @@ -18,7 +18,7 @@ public class PortalPlacer implements Listener { @SuppressWarnings("unused") private final AdvancedPortalsPlugin plugin; - private final boolean DISABLE_GATEWAY_BEAM; + //private final boolean DISABLE_GATEWAY_BEAM; public PortalPlacer(AdvancedPortalsPlugin plugin) { this.plugin = plugin; @@ -26,7 +26,7 @@ public class PortalPlacer implements Listener { ConfigAccessor config = new ConfigAccessor(plugin, "config.yml"); boolean portalPlace = config.getConfig().getBoolean("CanBuildPortalBlock"); - this.DISABLE_GATEWAY_BEAM = config.getConfig().getBoolean("DisableGatewayBeam", true); + //this.DISABLE_GATEWAY_BEAM = config.getConfig().getBoolean("DisableGatewayBeam", true); if (portalPlace) { @@ -50,18 +50,18 @@ public class PortalPlacer implements Listener { } else if (name.equals("\u00A78Gateway Block Placer")){ event.getBlockPlaced().setType(Material.END_GATEWAY); - if(this.DISABLE_GATEWAY_BEAM) { + /*if(this.DISABLE_GATEWAY_BEAM) { Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> { this.plugin.compat.setGatewayAgeHigh(event.getBlock()); }, 1); - } + }*/ } } } - @EventHandler + /*@EventHandler public void onChunkLoad(ChunkLoadEvent event) { if(!this.DISABLE_GATEWAY_BEAM) { return; @@ -71,7 +71,7 @@ public class PortalPlacer implements Listener { this.plugin.compat.setGatewayAgeHigh(block.getBlock()); } //event.getHandlers(); - } + }*/ @EventHandler(priority = EventPriority.HIGHEST) public void onBlockPhysics(BlockPhysicsEvent event) { diff --git a/src/main/java/com/sekwah/advancedportals/reflection/ReflectionHelper.java b/src/main/java/com/sekwah/advancedportals/reflection/ReflectionHelper.java new file mode 100644 index 0000000..4a7a8d4 --- /dev/null +++ b/src/main/java/com/sekwah/advancedportals/reflection/ReflectionHelper.java @@ -0,0 +1,17 @@ +package com.sekwah.advancedportals.reflection; + +import java.lang.reflect.Field; + +public class ReflectionHelper { + + public static Field getFieldByType(Class clazz, Class findingType, boolean isAccessable) { + Field[] fields = clazz.getDeclaredFields(); + for(Field field : fields) { + if(field.getType() == findingType && field.isAccessible() == isAccessable) { + field.setAccessible(true); + return field; + } + } + return null; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index aac0b82..3d50c78 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -69,7 +69,9 @@ BlockSpectatorMode: false PortalCooldown: 5 # How long after trying to enter a portal until the player can try to enter another. 0 or lower to deactivate. ThrowbackAmount: 0.7 # How fast to throw them back, 0 or lower to disable throwback -DisableGatewayBeam: true +# Experimental, works but not all the time (Entities don't seem to be provided in the event sometimes or something is wrong) +# We have no plans to further this but if anyone has any tips feel free to contact us here https://discord.gg/25QZVVn +DisableGatewayBeam: false # Letters are flags. Include them to activate. n always disables everything, remove if you want it to work. # Lettering may not make too much sense but meh its useful. Examples are "ocpk" or "cop" (doesnt matter order)