From c4d5a8e05d50b2459d691fc281ef097114887003 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 29 Mar 2022 20:47:33 +0200 Subject: [PATCH] Potentially faster TNT iteration Signed-off-by: Lixfel --- .../utils/CraftbukkitWrapper10.java | 7 +++++++ .../utils/CraftbukkitWrapper12.java | 7 +++++++ .../utils/CraftbukkitWrapper14.java | 7 +++++++ .../utils/CraftbukkitWrapper15.java | 7 +++++++ .../utils/CraftbukkitWrapper18.java | 8 ++++++++ .../utils/CraftbukkitWrapper8.java | 7 +++++++ .../utils/CraftbukkitWrapper9.java | 7 +++++++ .../src/de/steamwar/fightsystem/Config.java | 2 ++ .../fightsystem/listener/Recording.java | 19 +++++++++++-------- .../fightsystem/utils/CraftbukkitWrapper.java | 4 ++++ 10 files changed, 67 insertions(+), 8 deletions(-) diff --git a/FightSystem_10/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper10.java b/FightSystem_10/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper10.java index 722eba4..6e421a9 100644 --- a/FightSystem_10/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper10.java +++ b/FightSystem_10/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper10.java @@ -19,6 +19,7 @@ package de.steamwar.fightsystem.utils; +import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.fight.FightWorld; import net.minecraft.server.v1_10_R1.Chunk; import org.bukkit.World; @@ -32,6 +33,7 @@ import org.bukkit.inventory.ItemStack; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; public class CraftbukkitWrapper10 implements CraftbukkitWrapper.ICraftbukkitWrapper { @Override @@ -67,4 +69,9 @@ public class CraftbukkitWrapper10 implements CraftbukkitWrapper.ICraftbukkitWrap keys.remove("Damage"); return !keys.isEmpty(); } + + @Override + public Stream tntIterator() { + return ((CraftWorld) Config.world).getHandle().entityList.stream(); + } } diff --git a/FightSystem_12/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper12.java b/FightSystem_12/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper12.java index 37b70a5..d980c1c 100644 --- a/FightSystem_12/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper12.java +++ b/FightSystem_12/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper12.java @@ -19,6 +19,7 @@ package de.steamwar.fightsystem.utils; +import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.fight.FightWorld; import net.minecraft.server.v1_12_R1.Chunk; import org.bukkit.World; @@ -32,6 +33,7 @@ import org.bukkit.inventory.ItemStack; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; public class CraftbukkitWrapper12 implements CraftbukkitWrapper.ICraftbukkitWrapper { @Override @@ -67,4 +69,9 @@ public class CraftbukkitWrapper12 implements CraftbukkitWrapper.ICraftbukkitWrap keys.remove("Damage"); return !keys.isEmpty(); } + + @Override + public Stream tntIterator() { + return ((CraftWorld) Config.world).getHandle().entityList.stream(); + } } diff --git a/FightSystem_14/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper14.java b/FightSystem_14/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper14.java index 5c74382..c53bfe5 100644 --- a/FightSystem_14/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper14.java +++ b/FightSystem_14/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper14.java @@ -19,6 +19,7 @@ package de.steamwar.fightsystem.utils; +import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.fight.FightWorld; import net.minecraft.server.v1_14_R1.Chunk; import org.bukkit.World; @@ -32,6 +33,7 @@ import org.bukkit.inventory.ItemStack; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; public class CraftbukkitWrapper14 implements CraftbukkitWrapper.ICraftbukkitWrapper { @Override @@ -68,4 +70,9 @@ public class CraftbukkitWrapper14 implements CraftbukkitWrapper.ICraftbukkitWrap keys.remove("Damage"); return !keys.isEmpty(); } + + @Override + public Stream tntIterator() { + return ((CraftWorld) Config.world).getHandle().entitiesById.values().stream(); + } } diff --git a/FightSystem_15/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper15.java b/FightSystem_15/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper15.java index 03f40d6..6d7b376 100644 --- a/FightSystem_15/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper15.java +++ b/FightSystem_15/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper15.java @@ -19,6 +19,7 @@ package de.steamwar.fightsystem.utils; +import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.fight.FightWorld; import net.minecraft.server.v1_15_R1.Chunk; import org.bukkit.World; @@ -32,6 +33,7 @@ import org.bukkit.inventory.ItemStack; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; public class CraftbukkitWrapper15 implements CraftbukkitWrapper.ICraftbukkitWrapper { @Override @@ -68,4 +70,9 @@ public class CraftbukkitWrapper15 implements CraftbukkitWrapper.ICraftbukkitWrap keys.remove("Damage"); return !keys.isEmpty(); } + + @Override + public Stream tntIterator() { + return ((CraftWorld) Config.world).getHandle().entitiesById.values().stream(); + } } diff --git a/FightSystem_18/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper18.java b/FightSystem_18/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper18.java index c5dee09..8fd5f23 100644 --- a/FightSystem_18/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper18.java +++ b/FightSystem_18/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper18.java @@ -19,6 +19,7 @@ package de.steamwar.fightsystem.utils; +import de.steamwar.fightsystem.Config; import net.minecraft.world.level.chunk.Chunk; import org.bukkit.World; import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; @@ -31,6 +32,8 @@ import org.bukkit.inventory.ItemStack; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; public class CraftbukkitWrapper18 implements CraftbukkitWrapper.ICraftbukkitWrapper { @@ -60,4 +63,9 @@ public class CraftbukkitWrapper18 implements CraftbukkitWrapper.ICraftbukkitWrap keys.remove("Damage"); return !keys.isEmpty(); } + + @Override + public Stream tntIterator() { + return StreamSupport.stream(((CraftWorld) Config.world).getHandle().H().a().spliterator(), false); + } } diff --git a/FightSystem_8/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper8.java b/FightSystem_8/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper8.java index f79dc0e..deb612c 100644 --- a/FightSystem_8/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper8.java +++ b/FightSystem_8/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper8.java @@ -19,6 +19,7 @@ package de.steamwar.fightsystem.utils; +import de.steamwar.fightsystem.Config; import net.minecraft.server.v1_8_R3.Chunk; import org.bukkit.World; import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; @@ -31,6 +32,7 @@ import org.bukkit.inventory.ItemStack; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; public class CraftbukkitWrapper8 implements CraftbukkitWrapper.ICraftbukkitWrapper { @Override @@ -64,4 +66,9 @@ public class CraftbukkitWrapper8 implements CraftbukkitWrapper.ICraftbukkitWrapp keys.remove("Damage"); return !keys.isEmpty(); } + + @Override + public Stream tntIterator() { + return ((CraftWorld) Config.world).getHandle().entityList.stream(); + } } diff --git a/FightSystem_9/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper9.java b/FightSystem_9/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper9.java index 4a2cc0b..f1ace08 100644 --- a/FightSystem_9/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper9.java +++ b/FightSystem_9/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper9.java @@ -19,6 +19,7 @@ package de.steamwar.fightsystem.utils; +import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.fight.FightWorld; import net.minecraft.server.v1_9_R2.Chunk; import org.bukkit.World; @@ -32,6 +33,7 @@ import org.bukkit.inventory.ItemStack; import java.util.HashSet; import java.util.Set; +import java.util.stream.Stream; public class CraftbukkitWrapper9 implements CraftbukkitWrapper.ICraftbukkitWrapper { @Override @@ -67,4 +69,9 @@ public class CraftbukkitWrapper9 implements CraftbukkitWrapper.ICraftbukkitWrapp keys.remove("Damage"); return !keys.isEmpty(); } + + @Override + public Stream tntIterator() { + return ((CraftWorld) Config.world).getHandle().entityList.stream(); + } } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/Config.java b/FightSystem_Core/src/de/steamwar/fightsystem/Config.java index a97493d..020687c 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/Config.java @@ -41,6 +41,8 @@ public class Config { private Config(){} + public static final World world = Bukkit.getWorlds().get(0); + //Fight sequence public static final int NoPlayerOnlineDuration; public static final int PreSchemPasteDuration; diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/listener/Recording.java b/FightSystem_Core/src/de/steamwar/fightsystem/listener/Recording.java index b4f5621..03f7c82 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/listener/Recording.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/listener/Recording.java @@ -30,14 +30,12 @@ import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; import de.steamwar.fightsystem.states.StateDependentListener; import de.steamwar.fightsystem.states.StateDependentTask; -import de.steamwar.fightsystem.utils.BountifulWrapper; -import de.steamwar.fightsystem.utils.FlatteningWrapper; -import de.steamwar.fightsystem.utils.ProtocolAPI; -import de.steamwar.fightsystem.utils.SWSound; +import de.steamwar.fightsystem.utils.*; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.entity.TNTPrimed; @@ -108,13 +106,18 @@ public class Recording implements Listener { if(FightState.getFightState() == FightState.SPECTATE || !GlobalRecorder.getInstance().recording()) return; - for(TNTPrimed tnt : world.getEntitiesByClass(TNTPrimed.class)){ - GlobalRecorder.getInstance().entityMoves(tnt); - GlobalRecorder.getInstance().entitySpeed(tnt); - } + CraftbukkitWrapper.impl.tntIterator().filter(primedTnt::isInstance).forEach(this::trackEntity); }, 1, 1); } + private static final Class primedTnt = Reflection.getClass("{nms.world.entity}.EntityTNTPrimed"); + private static final Reflection.MethodInvoker getBukkitEntity = Reflection.getTypedMethod(Reflection.getClass("{nms.world.entity}.Entity"), "getBukkitEntity", null); + private void trackEntity(Object e) { + Entity entity = (Entity) getBukkitEntity.invoke(e); + GlobalRecorder.getInstance().entityMoves(entity); + GlobalRecorder.getInstance().entitySpeed(entity); + } + private static final Class blockDigPacket = Reflection.getClass("{nms.network.protocol.game}.PacketPlayInBlockDig"); private static final Class playerDigType = blockDigPacket.getDeclaredClasses()[0]; private static final Reflection.FieldAccessor blockDigType = Reflection.getField(blockDigPacket, playerDigType, 0); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper.java b/FightSystem_Core/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper.java index 7724d75..5df31ee 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/utils/CraftbukkitWrapper.java @@ -26,6 +26,8 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.util.stream.Stream; + public class CraftbukkitWrapper { private CraftbukkitWrapper() {} @@ -36,5 +38,7 @@ public class CraftbukkitWrapper { void sendResourcePack(Player player, String pack, String sha1); float headRotation(Entity e); boolean hasItems(ItemStack stack); + + Stream tntIterator(); } }