From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ivan Pekov <ivan@mrivanplays.com>
Date: Wed, 7 Oct 2020 17:38:05 +0300
Subject: [PATCH] Replace some hot streams


diff --git a/src/main/java/net/minecraft/server/ChunkRegionLoader.java b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
index 30d4589c9cdad11809fc09cb51235d6da8e03195..bf7d5394a59186aa516497e8eaae8e5000db6a62 100644
--- a/src/main/java/net/minecraft/server/ChunkRegionLoader.java
+++ b/src/main/java/net/minecraft/server/ChunkRegionLoader.java
@@ -430,9 +430,20 @@ public class ChunkRegionLoader {
 
         for (int i = -1; i < 17; ++i) { // Paper - conflict on loop parameter change
             int finalI = i; // CraftBukkit - decompile errors
+            // Yatopia start - replace stream
+            /*
             ChunkSection chunksection = (ChunkSection) Arrays.stream(achunksection).filter((chunksection1) -> {
                 return chunksection1 != null && chunksection1.getYPosition() >> 4 == finalI; // CraftBukkit - decompile errors
             }).findFirst().orElse(Chunk.a);
+            */
+            ChunkSection chunksection = Chunk.EMPTY_CHUNK_SECTION;
+            for (ChunkSection section : achunksection) {
+                if (section != null && section.getYPosition() >> 4 == i) {
+                    chunksection = section;
+                    break;
+                }
+            }
+            // Yatopia end
             // Paper start - async chunk save for unload
             NibbleArray nibblearray; // block light
             NibbleArray nibblearray1; // sky light
@@ -728,6 +739,8 @@ public class ChunkRegionLoader {
         while (iterator.hasNext()) {
             String s = (String) iterator.next();
 
+            // Yatopia start - replace stream
+            /*
             map.put(StructureGenerator.a.get(s.toLowerCase(Locale.ROOT)), new LongOpenHashSet(Arrays.stream(nbttagcompound1.getLongArray(s)).filter((i) -> {
                 ChunkCoordIntPair chunkcoordintpair1 = new ChunkCoordIntPair(i);
 
@@ -738,6 +751,22 @@ public class ChunkRegionLoader {
                     return true;
                 }
             }).toArray()));
+             */
+            long[] longArray = nbttagcompound1.getLongArray(s);
+            LongSet set = new LongOpenHashSet();
+            for (long value : longArray) {
+                // got from ChunkCoordIntPair
+                // avoid creating additional object
+                int x = (int) value;
+                int z = (int) (value >> 32);
+                if (Math.max(Math.abs(x - chunkcoordintpair.x), Math.abs(z - chunkcoordintpair.z)) > 8) {
+                    ChunkRegionLoader.LOGGER.warn("Found invalid structure reference [ {} @ {} ] for chunk {}. ", s, "[" + x + ", " + z + "]", chunkcoordintpair);
+                } else {
+                    set.add(value);
+                }
+            }
+            map.put(StructureGenerator.a.get(s.toLowerCase(Locale.ROOT)), set);
+            // Yatopia end
         }
 
         return map;
diff --git a/src/main/java/net/minecraft/server/DefinedStructure.java b/src/main/java/net/minecraft/server/DefinedStructure.java
index 8974d7944f159b9346680c639daf0f8c06767cfe..bd610bfcd9571d7478ed13a0c35bc959cab07f0f 100644
--- a/src/main/java/net/minecraft/server/DefinedStructure.java
+++ b/src/main/java/net/minecraft/server/DefinedStructure.java
@@ -811,9 +811,18 @@ public class DefinedStructure {
 
         public List<DefinedStructure.BlockInfo> a(Block block) {
             return (List) this.b.computeIfAbsent(block, (block1) -> {
+                // Yatopia start - replace stream
+                /*
                 return (List) this.a.stream().filter((definedstructure_blockinfo) -> {
                     return definedstructure_blockinfo.b.a(block1);
                 }).collect(Collectors.toList());
+                 */
+                List<DefinedStructure.BlockInfo> ret = new net.yatopia.server.list.GlueList<>();
+                for (BlockInfo info : a) {
+                    if (info.b.isBlock(block1)) { ret.add(info); }
+                }
+                return ret;
+                // Yatopia end
             });
         }
     }
diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index 3be9be2df7623ec1068f5782261d70fb34b2aa54..2f5bc2bde2f1231781b3738de57b4f9acc6113df 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -2529,6 +2529,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
         return this.world.getScoreboard().getPlayerTeam(this.getName());
     }
 
+    public final boolean isAlly(Entity entity) { return r(entity); } // Yatopia - OBFHELPER
     public boolean r(Entity entity) {
         return this.a(entity.getScoreboardTeam());
     }
diff --git a/src/main/java/net/minecraft/server/EntityPigZombie.java b/src/main/java/net/minecraft/server/EntityPigZombie.java
index a60bb54270b98bad9cc8caa9ce2538f54b03fbfe..3b9ba7c012586c7620e69cf450b8d1c1fee3b215 100644
--- a/src/main/java/net/minecraft/server/EntityPigZombie.java
+++ b/src/main/java/net/minecraft/server/EntityPigZombie.java
@@ -101,6 +101,8 @@ public class EntityPigZombie extends EntityZombie implements IEntityAngerable {
         double d0 = this.b(GenericAttributes.FOLLOW_RANGE);
         AxisAlignedBB axisalignedbb = AxisAlignedBB.a(this.getPositionVector()).grow(d0, 10.0D, d0);
 
+        // Yatopia start - replace stream
+        /*
         this.world.b(EntityPigZombie.class, axisalignedbb).stream().filter((entitypigzombie) -> {
             return entitypigzombie != this;
         }).filter((entitypigzombie) -> {
@@ -110,6 +112,13 @@ public class EntityPigZombie extends EntityZombie implements IEntityAngerable {
         }).forEach((entitypigzombie) -> {
             entitypigzombie.setGoalTarget(this.getGoalTarget(), org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_ATTACKED_NEARBY_ENTITY, true); // CraftBukkit
         });
+         */
+        for (EntityPigZombie zombie : world.b(EntityPigZombie.class, axisalignedbb)) {
+            if (zombie != this && zombie.getGoalTarget() == null && !zombie.isAlly(getGoalTarget())) {
+                zombie.setGoalTarget(getGoalTarget(), org.bukkit.event.entity.EntityTargetEvent.TargetReason.TARGET_ATTACKED_NEARBY_ENTITY, true);
+            }
+        }
+        // Yatopia end
     }
 
     private void fa() {
diff --git a/src/main/java/net/minecraft/server/EntityPlayer.java b/src/main/java/net/minecraft/server/EntityPlayer.java
index b43b02c0bdd5dbf0b7d30de90bdc2f74c015ecc8..b113d60408bcb77762587109ede7dc855d983254 100644
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
@@ -761,11 +761,20 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     private void eV() {
         AxisAlignedBB axisalignedbb = (new AxisAlignedBB(this.getChunkCoordinates())).grow(32.0D, 10.0D, 32.0D);
 
+        // Yatopia start - replace stream
+        /*
         this.world.b(EntityInsentient.class, axisalignedbb).stream().filter((entityinsentient) -> {
             return entityinsentient instanceof IEntityAngerable;
         }).forEach((entityinsentient) -> {
             ((IEntityAngerable) entityinsentient).b(this);
         });
+         */
+        for (EntityInsentient insentient : world.b(EntityInsentient.class, axisalignedbb)) {
+            if (insentient instanceof IEntityAngerable) {
+                insentient.b(this);
+            }
+        }
+        // Yatopia end
     }
 
     @Override
diff --git a/src/main/java/net/minecraft/server/EntityVillager.java b/src/main/java/net/minecraft/server/EntityVillager.java
index 939ef3bd6439a68dba4224661d050c6af0164178..ab3348a89b6864184d432e4a73fe02ac195b8629 100644
--- a/src/main/java/net/minecraft/server/EntityVillager.java
+++ b/src/main/java/net/minecraft/server/EntityVillager.java
@@ -678,11 +678,20 @@ public class EntityVillager extends EntityVillagerAbstract implements Reputation
             if (optional.isPresent()) {
                 WorldServer worldserver = (WorldServer) this.world;
 
+                // Yatopia start - replace stream
+                /*
                 ((List) optional.get()).stream().filter((entityliving) -> {
                     return entityliving instanceof ReputationHandler;
                 }).forEach((entityliving) -> {
                     worldserver.a(ReputationEvent.d, entity, (ReputationHandler) entityliving);
                 });
+                 */
+                for (EntityLiving livingEntity : optional.get()) {
+                    if (livingEntity instanceof ReputationHandler) {
+                        worldserver.a(ReputationEvent.d, entity, (ReputationHandler) livingEntity);
+                    }
+                }
+                // Yatopia end
             }
         }
     }
diff --git a/src/main/java/net/minecraft/server/InventorySubcontainer.java b/src/main/java/net/minecraft/server/InventorySubcontainer.java
index db5def8b1d4179a64b28366339a3634af593cd5e..8627fb85f5f8b3309caf681eede4c3455bdc618c 100644
--- a/src/main/java/net/minecraft/server/InventorySubcontainer.java
+++ b/src/main/java/net/minecraft/server/InventorySubcontainer.java
@@ -90,9 +90,19 @@ public class InventorySubcontainer implements IInventory, AutoRecipeOutput {
     }
 
     public List<ItemStack> f() {
+        // Yatopia start - replace stream
+        /*
         List<ItemStack> list = (List) this.items.stream().filter((itemstack) -> {
             return !itemstack.isEmpty();
         }).collect(Collectors.toList());
+         */
+        List<ItemStack> list = new net.yatopia.server.list.GlueList<>();
+        for (ItemStack item : items) {
+            if (!item.isEmpty()) {
+                list.add(item);
+            }
+        }
+        // Yatopia end
 
         this.clear();
         return list;
diff --git a/src/main/java/net/minecraft/server/LootTable.java b/src/main/java/net/minecraft/server/LootTable.java
index 9922c8b2a7fbb55cfb8b9cea9545c9ec0a0dcbf8..0e8c52f64dd3ffe5e9510227cc6cbaf5b38a5e87 100644
--- a/src/main/java/net/minecraft/server/LootTable.java
+++ b/src/main/java/net/minecraft/server/LootTable.java
@@ -85,7 +85,7 @@ public class LootTable {
     }
 
     public List<ItemStack> populateLoot(LootTableInfo loottableinfo) {
-        List<ItemStack> list = Lists.newArrayList();
+        List<ItemStack> list = new net.yatopia.server.list.GlueList<>(); // Yatopia
 
         this.populateLoot(loottableinfo, list::add);
         return list;
@@ -122,7 +122,15 @@ public class LootTable {
         if (event.isCancelled()) {
             return;
         }
+        // Yatopia start - replace stream
+        /*
         list = event.getLoot().stream().map(CraftItemStack::asNMSCopy).collect(Collectors.toList());
+         */
+        list = new net.yatopia.server.list.GlueList<>();
+        for (org.bukkit.inventory.ItemStack bukkitItem : event.getLoot()) {
+            list.add(CraftItemStack.asNMSCopy(bukkitItem));
+        }
+        // Yatopia end
         // CraftBukkit end
         List<Integer> list1 = this.a(iinventory, random);
 
diff --git a/src/main/java/net/minecraft/server/PiglinAI.java b/src/main/java/net/minecraft/server/PiglinAI.java
index 0407fa1751d89a037da8cb01f5ceef9b9833dd18..a4e98b11e4ce4a4c9f3aa0986b7b2018b1f24f33 100644
--- a/src/main/java/net/minecraft/server/PiglinAI.java
+++ b/src/main/java/net/minecraft/server/PiglinAI.java
@@ -359,6 +359,8 @@ public class PiglinAI {
     public static void a(EntityHuman entityhuman, boolean flag) {
         List<EntityPiglinAbstract> list = entityhuman.world.a(EntityPiglin.class, entityhuman.getBoundingBox().g(16.0D)); // CraftBukkit - decompile error
 
+        // Yatopia start - replace stream
+        /*
         list.stream().filter(PiglinAI::d).filter((entitypiglin) -> {
             return !flag || BehaviorUtil.c(entitypiglin, entityhuman);
         }).forEach((entitypiglin) -> {
@@ -369,6 +371,17 @@ public class PiglinAI {
             }
 
         });
+         */
+        for (EntityPiglinAbstract piglinAbstract : list) {
+            if (PiglinAI.d(piglinAbstract) && (!flag || BehaviorUtil.c(piglinAbstract, entityhuman))) {
+                if (piglinAbstract.world.getGameRules().getBoolean(GameRules.UNIVERSAL_ANGER)) {
+                    d(piglinAbstract, entityhuman);
+                } else {
+                    c(piglinAbstract, entityhuman);
+                }
+            }
+        }
+        // Yatopia end
     }
 
     public static EnumInteractionResult a(EntityPiglin entitypiglin, EntityHuman entityhuman, EnumHand enumhand) {
diff --git a/src/main/java/net/minecraft/server/Raid.java b/src/main/java/net/minecraft/server/Raid.java
index 89464cc770b4875ad65c5e84315e462689cea57b..ed13f8fe725ce7601e5c506b590042cedbac7b54 100644
--- a/src/main/java/net/minecraft/server/Raid.java
+++ b/src/main/java/net/minecraft/server/Raid.java
@@ -794,7 +794,16 @@ public class Raid {
 
     // CraftBukkit start - a method to get all raiders
     public java.util.Collection<EntityRaider> getRaiders() {
+        // Yatopia start - replace stream
+        /*
         return this.raiders.values().stream().flatMap(Set::stream).collect(java.util.stream.Collectors.toSet());
+         */
+        java.util.List<EntityRaider> ret = new net.yatopia.server.list.GlueList<>();
+        for (Set<EntityRaider> raid : raiders.values()) {
+            ret.addAll(raid);
+        }
+        return ret;
+        // Yatopia end
     }
     // CraftBukkit end
 
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
index b739e19cb50e4f86f1cb100b88cf91fbbbf796b4..e41f0ae32f66e5bf3b5ca1e10be2293f945bb5b1 100644
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
@@ -60,11 +60,11 @@ public class WorldServer extends World implements GeneratorAccessSeed {
     public final Int2ObjectMap<Entity> entitiesById = new Int2ObjectLinkedOpenHashMap(); final com.tuinity.tuinity.util.maplist.IteratorSafeOrderedReferenceSet<Entity> entitiesForIteration = new com.tuinity.tuinity.util.maplist.IteratorSafeOrderedReferenceSet<>(2048, 0.5f, 2048, 0.2, true); // Tuinity - make removing entities while ticking safe
     private final Map<UUID, Entity> entitiesByUUID = Maps.newHashMap();
     private final Queue<Entity> entitiesToAdd = Queues.newArrayDeque();
-    public final List<EntityPlayer> players = Lists.newArrayList(); // Paper - private -> public
+    public final List<EntityPlayer> players = new net.yatopia.server.list.GlueList<>(); // Paper - private -> public // Yatopia
     public final ChunkProviderServer chunkProvider; // Paper - public
     boolean tickingEntities;
     // Paper start
-    List<java.lang.Runnable> afterEntityTickingTasks = Lists.newArrayList();
+    List<java.lang.Runnable> afterEntityTickingTasks = new net.yatopia.server.list.GlueList<>(); // Yatopia
     public void doIfNotEntityTicking(java.lang.Runnable run) {
         if (tickingEntities) {
             afterEntityTickingTasks.add(run);
@@ -828,6 +828,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
         }
         // */
         // Yatopia start
+        boolean sleepyMatch = true;
         for (EntityPlayer player : players) {
             if (player.world == this) {
                 player.tickWeather();
@@ -835,6 +836,11 @@ public class WorldServer extends World implements GeneratorAccessSeed {
                 player.updateWeather(this.lastRainLevel, this.rainLevel, this.lastThunderLevel, this.thunderLevel);
                 // Yatopia end
             }
+            // Yatopia start
+            if (sleepyMatch && !player.isSpectator() && !player.isDeeplySleeping() && !player.fauxSleeping) {
+                sleepyMatch = false;
+            }
+            // Yatopia end
         }
 
         /* // Yatopia start - moved up
@@ -854,9 +860,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
         // CraftBukkit end
          */ // Yatopia end
 
-        if (this.everyoneSleeping && this.players.stream().noneMatch((entityplayer) -> {
-            return !entityplayer.isSpectator() && !entityplayer.isDeeplySleeping() && !entityplayer.fauxSleeping; // CraftBukkit
-        })) {
+        if (this.everyoneSleeping && sleepyMatch) { // Yatopia
             // CraftBukkit start
             long l = this.worldData.getDayTime() + 24000L;
             TimeSkipEvent event = new TimeSkipEvent(this.getWorld(), TimeSkipEvent.SkipReason.NIGHT_SKIP, (l - l % 24000L) - this.getDayTime());
@@ -1026,9 +1030,9 @@ public class WorldServer extends World implements GeneratorAccessSeed {
     }
 
     private void wakeupPlayers() {
-        (this.players.stream().filter(EntityLiving::isSleeping).collect(Collectors.toList())).forEach((entityplayer) -> { // CraftBukkit - decompile error
+        for (EntityPlayer entityplayer : players) { if (entityplayer.isSleeping()) { // Yatopia
             entityplayer.wakeup(false, false);
-        });
+        }} // Yatopia
     }
 
     // Paper start - optimise random block ticking
@@ -1794,8 +1798,9 @@ public class WorldServer extends World implements GeneratorAccessSeed {
         // Spigot start
         if ( entity instanceof EntityHuman )
         {
-            this.getMinecraftServer().worldServer.values().stream().map( WorldServer::getWorldPersistentData ).forEach( (worldData) ->
+            for ( WorldServer worldServer : getMinecraftServer().worldServer.values() ) // Yatopia
             {
+                WorldPersistentData worldData = worldServer.getWorldPersistentData(); // Yatopia
                 for (Object o : worldData.data.values() )
                 {
                     if ( o instanceof WorldMap )
@@ -1812,7 +1817,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
                         }
                     }
                 }
-            } );
+            } // Yatopia
         }
         // Spigot end
         // Spigot Start
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftRaid.java b/src/main/java/org/bukkit/craftbukkit/CraftRaid.java
index 360a4901f5159a3f0606214f1abf2f8cee2ecbe1..450a64396f8fe167bc0accb7fdfcfeebb16351ac 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftRaid.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftRaid.java
@@ -91,11 +91,20 @@ public final class CraftRaid implements Raid {
 
     @Override
     public List<Raider> getRaiders() {
+        // Yatopia start - replace stream
+        /*
         return handle.getRaiders().stream().map(new Function<EntityRaider, Raider>() {
             @Override
             public Raider apply(EntityRaider entityRaider) {
                 return (Raider) entityRaider.getBukkitEntity();
             }
         }).collect(ImmutableList.toImmutableList());
+         */
+        List<Raider> ret = new net.yatopia.server.list.GlueList<>();
+        for (EntityRaider raider : handle.getRaiders()) {
+            ret.add((Raider) raider.getBukkitEntity());
+        }
+        return Collections.unmodifiableList(ret);
+        // Yatopia end
     }
 }
