From 698a03fe533c795e57b1f30f0099438699fc034b Mon Sep 17 00:00:00 2001 From: tr7zw Date: Sun, 22 Mar 2020 20:42:24 +0100 Subject: [PATCH] Rewrite BehaviorController with 99% less Streams --- .../minecraft/server/BehaviorController.java | 250 ++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 src/main/java/net/minecraft/server/BehaviorController.java diff --git a/src/main/java/net/minecraft/server/BehaviorController.java b/src/main/java/net/minecraft/server/BehaviorController.java new file mode 100644 index 000000000..1bb496b3e --- /dev/null +++ b/src/main/java/net/minecraft/server/BehaviorController.java @@ -0,0 +1,250 @@ +package net.minecraft.server; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Stream; + +import javax.annotation.Nullable; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import com.mojang.datafixers.Dynamic; +import com.mojang.datafixers.types.DynamicOps; +import com.mojang.datafixers.util.Pair; + +import net.minecraft.server.Behavior.Status; + +public class BehaviorController implements MinecraftSerializable { + private final Map, Optional> memories = Maps.newHashMap(); + private final Map>, Sensor> sensors = Maps.newLinkedHashMap(); + private final Map>>> c = Maps.newTreeMap(); + private Schedule schedule; + private final Map, MemoryStatus>>> e; + private Set f; + private final Set g; + private Activity h; + private long i; + + public BehaviorController(Collection> var0, + Collection>> var1, Dynamic var2) { + this.schedule = Schedule.EMPTY; + this.e = Maps.newHashMap(); + this.f = Sets.newHashSet(); + this.g = Sets.newHashSet(); + this.h = Activity.IDLE; + this.i = -9999L; + for(MemoryModuleType mem : var0) { + this.memories.put(mem, Optional.empty()); + } + for(SensorType> sensor : var1) { + this.sensors.put(sensor, sensor.a()); + } + for(Sensor sensor : sensors.values()) { + Iterator> var2x = sensor.a().iterator(); + + while (var2x.hasNext()) { + MemoryModuleType next = var2x.next(); + this.memories.put(next, Optional.empty()); + } + } + + Iterator, Dynamic>> var4 = var2.get("memories").asMap(Function.identity(), Function.identity()).entrySet().iterator(); + + while (var4.hasNext()) { + Entry, Dynamic> var5 = var4.next(); + this.a(IRegistry.MEMORY_MODULE_TYPE + .get(new MinecraftKey(( var5.getKey()).asString(""))), var5.getValue()); + } + + } + + public boolean hasMemory(MemoryModuleType var0) { + return this.a(var0, MemoryStatus.VALUE_PRESENT); + } + + private void a(MemoryModuleType var0, Dynamic var1) { + this.setMemory(var0, (U)((Function) var0.getSerializer().orElseThrow(RuntimeException::new)).apply(var1)); + } + + public void removeMemory(MemoryModuleType var0) { + this.setMemory(var0, Optional.empty()); + } + + public void setMemory(MemoryModuleType var0, @Nullable U var1) { + this.setMemory(var0, Optional.ofNullable(var1)); + } + + public void setMemory(MemoryModuleType var0, Optional var1) { + if (this.memories.containsKey(var0)) { + if (var1.isPresent() && this.a(var1.get())) { + this.removeMemory(var0); + } else { + this.memories.put(var0, var1); + } + } + + } + + public Optional getMemory(MemoryModuleType var0) { + return (Optional) this.memories.get(var0); + } + + public boolean a(MemoryModuleType var0, MemoryStatus var1) { + Optional var2 = this.memories.get(var0); + if (var2 == null) { + return false; + } else { + return var1 == MemoryStatus.REGISTERED || var1 == MemoryStatus.VALUE_PRESENT && var2.isPresent() + || var1 == MemoryStatus.VALUE_ABSENT && !var2.isPresent(); + } + } + + public Schedule getSchedule() { + return this.schedule; + } + + public void setSchedule(Schedule var0) { + this.schedule = var0; + } + + public void a(Set var0) { + this.f = var0; + } + + @Deprecated + public Stream> d() { + return this.c.values().stream().flatMap((var0) -> { + return var0.values().stream(); + }).flatMap(Collection::stream).filter((var0) -> { + return var0.a() == Status.RUNNING; + }); + } + + public void a(Activity var0) { + this.g.clear(); + this.g.addAll(this.f); + boolean var1 = this.e.keySet().contains(var0) && this.d(var0); + this.g.add(var1 ? var0 : this.h); + } + + public void a(long var0, long var2) { + if (var2 - this.i > 20L) { + this.i = var2; + Activity var4 = this.getSchedule().a((int) (var0 % 24000L)); + if (!this.g.contains(var4)) { + this.a(var4); + } + } + + } + + public void b(Activity var0) { + this.h = var0; + } + + public void a(Activity var0, ImmutableList>> var1) { + this.a((Activity) var0, var1, ImmutableSet.of()); + } + + public void a(Activity var0, ImmutableList>> var1, + Set, MemoryStatus>> var2) { + this.e.put(var0, var2); + for(Pair> var1x : var1) { + ((this.c.computeIfAbsent(var1x.getFirst(), (var0x) -> { + return Maps.newHashMap(); + })).computeIfAbsent(var0, (var0x) -> { + return Sets.newLinkedHashSet(); + })).add(var1x.getSecond()); + } + } + + public boolean c(Activity var0) { + return this.g.contains(var0); + } + + public BehaviorController f() { + BehaviorController var0 = new BehaviorController(this.memories.keySet(), this.sensors.keySet(), + new Dynamic<>(DynamicOpsNBT.a, new NBTTagCompound())); + for(Entry, Optional> entry : this.memories.entrySet()) { + entry.getValue().ifPresent((var2x) -> { + var0.memories.put(entry.getKey(), Optional.of(var2x)); + }); + } + return var0; + } + + public void a(WorldServer var0, E var1) { + this.c(var0, var1); + this.d(var0, var1); + this.e(var0, var1); + } + + public void b(WorldServer var0, E var1) { + long var2 = var1.world.getTime(); + this.d().forEach((var4) -> { + var4.e(var0, var1, var2); + }); + } + + public T a(DynamicOps var0) { + Map outMap = new HashMap(); + for(Entry, Optional> memory : this.memories.entrySet()) { + if((memory.getKey()).getSerializer().isPresent() && (memory.getValue()).isPresent()) { + outMap.put(var0.createString(IRegistry.MEMORY_MODULE_TYPE.getKey(memory.getKey()).toString()), ((MinecraftSerializable) (memory.getValue()).get()).a(var0)); + } + } + return var0.createMap(ImmutableMap.of(var0.createString("memories"), var0.createMap(outMap))); + } + + private void c(WorldServer var0, E var1) { + for(Sensor var2 : this.sensors.values()) { + var2.b(var0, var1); + } + } + + private void d(WorldServer var0, E var1) { + long var2 = var0.getTime(); + for(Map>> value : this.c.values()) { + for(Entry>> entry : value.entrySet()) { + if(this.g.contains(entry.getKey())) { + for(Behavior behavior : entry.getValue()) { + if(behavior.a() == Status.STOPPED) { + behavior.b(var0, var1, var2); + } + } + } + } + } + } + + private void e(WorldServer var0, E var1) { + long var2 = var0.getTime(); + this.d().forEach((var4) -> { + var4.c(var0, var1, var2); + }); + } + + private boolean d(Activity var0) { + for(Pair, MemoryStatus> entry : this.e.get(var0)) { + MemoryModuleType var1 = entry.getFirst(); + MemoryStatus var2 = entry.getSecond(); + if(!this.a(var1, var2)) { + return false; + } + } + return true; + } + + private boolean a(Object var0) { + return var0 instanceof Collection && ((Collection) var0).isEmpty(); + } +} \ No newline at end of file -- 2.25.1.windows.1