diff --git a/src/de/steamwar/spectatesystem/FightfileConnection.java b/src/de/steamwar/spectatesystem/FightfileConnection.java index 801cea5..32e1496 100644 --- a/src/de/steamwar/spectatesystem/FightfileConnection.java +++ b/src/de/steamwar/spectatesystem/FightfileConnection.java @@ -1,15 +1,32 @@ package de.steamwar.spectatesystem; -import java.io.*; +import org.bukkit.Bukkit; + +import java.io.DataInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.logging.Level; +import java.util.zip.GZIPInputStream; public class FightfileConnection extends PacketSource { public FightfileConnection(File fightFile) throws IOException { - super(new DataInputStream(new FileInputStream(fightFile))); + super(new DataInputStream(new GZIPInputStream(new FileInputStream(fightFile)))); } @Override boolean isClosed() { + try{ + return inputStream.available() == 0; + }catch (IOException e){ + Bukkit.getLogger().log(Level.WARNING, "On close test exception", e); + return true; + } + } + + @Override + boolean async() { return false; } } diff --git a/src/de/steamwar/spectatesystem/FightserverConnection.java b/src/de/steamwar/spectatesystem/FightserverConnection.java index c7065e9..6fa7447 100644 --- a/src/de/steamwar/spectatesystem/FightserverConnection.java +++ b/src/de/steamwar/spectatesystem/FightserverConnection.java @@ -48,4 +48,9 @@ public class FightserverConnection extends PacketSource { public boolean isClosed() { return socket.isClosed(); } + + @Override + boolean async() { + return true; + } } diff --git a/src/de/steamwar/spectatesystem/PacketProcessor.java b/src/de/steamwar/spectatesystem/PacketProcessor.java index 7171194..e7f8e2d 100644 --- a/src/de/steamwar/spectatesystem/PacketProcessor.java +++ b/src/de/steamwar/spectatesystem/PacketProcessor.java @@ -35,6 +35,7 @@ import net.minecraft.server.v1_15_R1.Block; import org.bukkit.*; import org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; import java.io.EOFException; import java.io.IOException; @@ -49,10 +50,14 @@ class PacketProcessor { private final Map packetCounter = new HashMap<>(); private final PacketSource source; + private final BukkitTask task; public PacketProcessor(PacketSource source){ this.source = source; - Bukkit.getScheduler().runTaskAsynchronously(SpectateSystem.get(), this::process); + if(source.async()) + task = Bukkit.getScheduler().runTaskAsynchronously(SpectateSystem.get(), this::process); + else + task = Bukkit.getScheduler().runTaskTimer(SpectateSystem.get(), this::process, 1, 1); } private void playerJoins() throws IOException{ @@ -259,9 +264,25 @@ class PacketProcessor { SpectateSystem.getScoreboard().addValue(key, value); } + private void endSpectating(){ + WorldLoader.reloadWorld(); + SpectateSystem.getScoreboard().setTitle("§eKein Kampf"); + + for(Map.Entry entry : packetCounter.entrySet()){ + System.out.println(Integer.toHexString(entry.getKey()) + " " + entry.getValue()); + } + System.out.println("Techhiderupdates: " + debug_block_ignored + ", Unneccesary: " + debug_block_unneccesary); + + packetCounter.clear(); + debug_block_ignored = 0; + debug_block_unneccesary = 0; + } + private void process(){ + try{ - while(!source.isClosed()){ + boolean tickFinished = false; + while(!source.isClosed() && !tickFinished){ byte packetType = source.rByte(); packetCounter.compute(packetType, (key, value) -> value != null ? value + 1 : 1); switch(packetType){ @@ -340,6 +361,8 @@ class PacketProcessor { break; case (byte) 0xFF: //Tick + if(!source.async()) + tickFinished = true; break; default: Bukkit.getLogger().log(Level.SEVERE, "Unknown packet recieved, closing"); @@ -354,12 +377,10 @@ class PacketProcessor { source.close(); } - WorldLoader.reloadWorld(); - SpectateSystem.getScoreboard().setTitle("§eKein Kampf"); - - for(Map.Entry entry : packetCounter.entrySet()){ - System.out.println(Integer.toHexString(entry.getKey()) + " " + entry.getValue()); + if(source.isClosed()){ + endSpectating(); + if(!source.async()) + task.cancel(); } - System.out.println("Techhiderupdates: " + debug_block_ignored + ", Unneccesary: " + debug_block_unneccesary); } } diff --git a/src/de/steamwar/spectatesystem/PacketSource.java b/src/de/steamwar/spectatesystem/PacketSource.java index 4ac291a..46e503b 100644 --- a/src/de/steamwar/spectatesystem/PacketSource.java +++ b/src/de/steamwar/spectatesystem/PacketSource.java @@ -27,7 +27,7 @@ import java.util.logging.Level; public abstract class PacketSource { - private final DataInputStream inputStream; + protected final DataInputStream inputStream; protected PacketSource(DataInputStream inputStream){ this.inputStream = inputStream; @@ -75,4 +75,5 @@ public abstract class PacketSource { } abstract boolean isClosed(); + abstract boolean async(); } diff --git a/src/de/steamwar/spectatesystem/SpectateSystem.java b/src/de/steamwar/spectatesystem/SpectateSystem.java index 403f052..870eab3 100644 --- a/src/de/steamwar/spectatesystem/SpectateSystem.java +++ b/src/de/steamwar/spectatesystem/SpectateSystem.java @@ -26,6 +26,7 @@ import de.steamwar.spectatesystem.listener.JoinListener; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; import java.io.IOException; import java.util.logging.Level; @@ -47,6 +48,15 @@ public class SpectateSystem extends JavaPlugin { } catch (IOException e) { Bukkit.getLogger().log(Level.SEVERE, "Could not open ConnectionAcceptor", e); } + + String fightFile = System.getProperty("fightFile"); + if(fightFile != null){ + try { + new FightfileConnection(new File(fightFile)); + } catch (IOException e) { + Bukkit.getLogger().log(Level.WARNING, "Could not start reading file", e); + } + } } @Override diff --git a/src/de/steamwar/spectatesystem/util/WorldLoader.java b/src/de/steamwar/spectatesystem/util/WorldLoader.java index badb587..1cd4373 100644 --- a/src/de/steamwar/spectatesystem/util/WorldLoader.java +++ b/src/de/steamwar/spectatesystem/util/WorldLoader.java @@ -22,7 +22,6 @@ package de.steamwar.spectatesystem.util; import de.steamwar.spectatesystem.Config; import de.steamwar.spectatesystem.SpectateSystem; import de.steamwar.spectatesystem.elements.REntity; -import net.minecraft.server.v1_15_R1.Block; import net.minecraft.server.v1_15_R1.BlockPosition; import net.minecraft.server.v1_15_R1.IBlockData; import net.minecraft.server.v1_15_R1.WorldServer; @@ -56,7 +55,7 @@ public class WorldLoader { for(int z = cZ; z < (cZ + 1) * 16; z++){ BlockPosition blockPos = new BlockPosition(x, y, z); IBlockData backupBlock = backup.getType(blockPos); - if(Block.REGISTRY_ID.getId(backupBlock) == Block.REGISTRY_ID.getId(cchunk.getType(blockPos))) + if(backupBlock == cworld.getType(blockPos)) continue; changedBlocks++; diff --git a/src/plugin.yml b/src/plugin.yml index 0500072..9a4ff66 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -4,7 +4,7 @@ name: SpectateSystem version: "1.0" api-version: "1.13" main: de.steamwar.spectatesystem.SpectateSystem -load: STARTUP +load: POSTWORLD depends: - SpigotCore - WorldEdit \ No newline at end of file