From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MrIvanPlays Date: Sun, 9 Aug 2020 22:27:52 +0300 Subject: [PATCH] Optimize BehaviorController diff --git a/src/main/java/net/minecraft/world/entity/ai/BehaviorController.java b/src/main/java/net/minecraft/world/entity/ai/BehaviorController.java index fc4216cafbd3a81cac319b11ee093bec38827742..e38cdf81e3f3ba1a2ee86c3a202e5643dd9c0fd1 100644 --- a/src/main/java/net/minecraft/world/entity/ai/BehaviorController.java +++ b/src/main/java/net/minecraft/world/entity/ai/BehaviorController.java @@ -65,13 +65,16 @@ public class BehaviorController { mutableobject.setValue((new MapCodec>() { public Stream keys(DynamicOps dynamicops) { - return collection.stream().flatMap((memorymoduletype) -> { - return SystemUtils.a(memorymoduletype.getSerializer().map((codec) -> { - return IRegistry.MEMORY_MODULE_TYPE.getKey(memorymoduletype); - })); - }).map((minecraftkey) -> { - return dynamicops.createString(minecraftkey.toString()); - }); + // Yatopia start + // about previous impl: WHY ? WHO DID THIS TO YOU? + Collection candidates = new java.util.ArrayList<>(); + for (MemoryModuleType moduleType : collection) { + moduleType.getSerializer() + .map(c -> IRegistry.MEMORY_MODULE_TYPE.getKey(moduleType)) + .ifPresent(key -> candidates.add(dynamicops.createString(key.toString()))); + } + return candidates.stream(); + // Yatopia end } public DataResult> decode(DynamicOps dynamicops, MapLike maplike) { @@ -83,23 +86,17 @@ public class BehaviorController { return this.a(memorymoduletype, dynamicops, pair.getSecond()); }); - mutableobject1.setValue(((DataResult) mutableobject1.getValue()).apply2(Builder::add, dataresult1)); + mutableobject1.setValue((mutableobject1.getValue()).apply2(Builder::add, dataresult1)); // Yatopia - decompile fix }); - DataResult dataresult = (DataResult) mutableobject1.getValue(); - Logger logger = BehaviorController.LOGGER; + DataResult>> dataresult = mutableobject1.getValue(); // Yatopia - decompile fix - logger.getClass(); - ImmutableList> immutablelist = (ImmutableList) dataresult.resultOrPartial(logger::error).map(Builder::build).orElseGet(ImmutableList::of); - Collection collection2 = collection; - Collection collection3 = collection1; - MutableObject mutableobject2 = mutableobject; + ImmutableList> immutablelist = dataresult.resultOrPartial(BehaviorController.LOGGER::error).map(Builder::build).orElseGet(ImmutableList::of); // Yatopia - decompile fix - mutableobject.getClass(); - return DataResult.success(new BehaviorController<>(collection2, collection3, immutablelist, mutableobject2::getValue)); + return DataResult.success(new BehaviorController<>(collection, collection1, immutablelist, mutableobject::getValue)); // Yatopia - decompile fix } private DataResult> a(MemoryModuleType memorymoduletype, DynamicOps dynamicops, T t0) { - return ((DataResult) memorymoduletype.getSerializer().map(DataResult::success).orElseGet(() -> { + return (memorymoduletype.getSerializer().map(DataResult::success).orElseGet(() -> { // Yatopia - decompile fix return DataResult.error("No codec for memory: " + memorymoduletype); })).flatMap((codec) -> { return codec.parse(dynamicops, t0); @@ -109,9 +106,9 @@ public class BehaviorController { } public RecordBuilder encode(BehaviorController behaviorcontroller, DynamicOps dynamicops, RecordBuilder recordbuilder) { - behaviorcontroller.j().forEach((behaviorcontroller_a) -> { + for (BehaviorController.a behaviorcontroller_a : behaviorcontroller.memoriesList()) { // Yatopia behaviorcontroller_a.a(dynamicops, recordbuilder); - }); + } // Yatopia return recordbuilder; } }).fieldOf("memories").codec()); @@ -170,6 +167,15 @@ public class BehaviorController { return ((Codec) this.b.get()).encodeStart(dynamicops, this); } + // Yatopia start + private List> memoriesList() { + List> ret = new java.util.ArrayList<>(); + for (Map.Entry, Optional>> entry : memories.entrySet()) { + ret.add(BehaviorController.a.b(entry.getKey(), (Optional) entry.getValue())); + } + return ret; + } + // Yatopia end private Stream> j() { return this.memories.entrySet().stream().map((entry) -> { return BehaviorController.a.b((MemoryModuleType) entry.getKey(), (Optional) entry.getValue()); @@ -208,7 +214,7 @@ public class BehaviorController { } public Optional getMemory(MemoryModuleType memorymoduletype) { - return ((Optional) this.memories.get(memorymoduletype)).map(ExpirableMemory::c); + return (Optional) this.memories.get(memorymoduletype).map(ExpirableMemory::c); // Yatopia - Decompile fix } public boolean b(MemoryModuleType memorymoduletype, U u0) { @@ -547,7 +553,7 @@ public class BehaviorController { private final MemoryModuleType a; private final Optional> b; - private static BehaviorController.a b(MemoryModuleType memorymoduletype, Optional> optional) { + private static BehaviorController.a b(MemoryModuleType memorymoduletype, Optional> optional) { // Yatopia - decompile fix return new BehaviorController.a<>(memorymoduletype, optional); }