diff --git a/src/main/java/com/pancake/surviving_the_aftermath/SurvivingTheAftermath.java b/src/main/java/com/pancake/surviving_the_aftermath/SurvivingTheAftermath.java index 677eafb..4ada4cd 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/SurvivingTheAftermath.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/SurvivingTheAftermath.java @@ -15,6 +15,9 @@ import com.pancake.surviving_the_aftermath.common.init.*; import com.pancake.surviving_the_aftermath.common.raid.NetherRaid; import com.pancake.surviving_the_aftermath.common.raid.module.NetherRaidModule; +import com.pancake.surviving_the_aftermath.common.tracker.MobBattleTracker; +import com.pancake.surviving_the_aftermath.common.tracker.RaidMobBattleTracker; +import com.pancake.surviving_the_aftermath.common.tracker.RaidPlayerBattleTracker; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.AddReloadListenerEvent; @@ -61,7 +64,9 @@ private void commonSetup(final FMLCommonSetupEvent event) { instance.registerWeightedListModule(EntityTypeWeightedListModule.IDENTIFIER, EntityTypeWeightedListModule.class); instance.registerWeightedListModule(ItemWeightedListModule.IDENTIFIER, ItemWeightedListModule.class); -// instance.registerTracker(PlayerBattleTracker.IDENTIFIER,PlayerBattleTracker.class); + instance.registerTracker(RaidMobBattleTracker.IDENTIFIER,RaidMobBattleTracker.class); + instance.registerTracker(RaidPlayerBattleTracker.IDENTIFIER,RaidPlayerBattleTracker.class); + instance.registerTracker(MobBattleTracker.IDENTIFIER,MobBattleTracker.class); instance.registerAmountModule(FixedAmountModule.IDENTIFIER,FixedAmountModule.class); instance.registerAmountModule(RandomAmountModule.IDENTIFIER,RandomAmountModule.class); diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/ITracker.java b/src/main/java/com/pancake/surviving_the_aftermath/api/ITracker.java index 9ff859e..ba921bb 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/ITracker.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/ITracker.java @@ -16,5 +16,5 @@ static void unregister(ITracker tracker) { MinecraftForge.EVENT_BUS.unregister(tracker); } - public void setUUID(UUID uuid); + public ITracker setUUID(UUID uuid); } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/aftermath/AftermathAPI.java b/src/main/java/com/pancake/surviving_the_aftermath/api/aftermath/AftermathAPI.java index 1ee21cc..4fab04c 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/aftermath/AftermathAPI.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/aftermath/AftermathAPI.java @@ -11,10 +11,8 @@ import org.slf4j.Logger; import java.lang.reflect.InvocationTargetException; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Random; +import java.util.*; +import java.util.stream.Collectors; public class AftermathAPI { private final Map> AFTERMATH_MODULE_MAP = Maps.newHashMap(); @@ -43,15 +41,25 @@ public Optional getRandomAftermathModule(String identifier) { return Optional.ofNullable(aftermathModules.get(new Random().nextInt(aftermathModules.size()))); } + public List getAftermathModules(String identifier) { + return AFTERMATH_MODULE_MAP.get(identifier); + } + public void registerTracker(String identifier, Class tracker) { TRACKERS.put(identifier, tracker); } - public ITracker getTracker(String identifier) { - return getObjectInstance(TRACKERS, identifier); + public List getTracker(UUID uuid, String... identifiers) { + return Arrays.stream(identifiers) + .map(identifier -> getObjectInstance(TRACKERS, identifier)) + .filter(Objects::nonNull) + .map(tracker -> tracker.setUUID(uuid)) + .toList(); } + + public void registerAmountModule(String identifier, Class amountModule) { AMOUNT_MODULES_MAP.put(identifier, amountModule); } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseAftermath.java b/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseAftermath.java index 92d1e58..30b83bf 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseAftermath.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseAftermath.java @@ -26,12 +26,11 @@ import java.util.Set; import java.util.UUID; import java.util.function.Predicate; -import java.util.function.Supplier; public abstract class BaseAftermath implements IAftermath { protected static final Logger LOGGER = LogUtils.getLogger(); - protected final AftermathAPI AFTERMATH_API = AftermathAPI.getInstance(); - protected final AftermathManager AFTERMATH_MANAGER = AftermathManager.getInstance(); + protected final AftermathAPI API = AftermathAPI.getInstance(); + protected final AftermathManager MANAGER = AftermathManager.getInstance(); protected final List TRACKERS = Lists.newArrayList(); protected AftermathState state; protected ServerLevel level; @@ -44,8 +43,8 @@ public abstract class BaseAftermath implements IA protected float progressPercent = progress.getProgress(); public BaseAftermath(ServerLevel level) { this.level = level; - this.module = (T) AFTERMATH_API.getRandomAftermathModule(getUniqueIdentifier()) - .orElseGet(() -> AFTERMATH_API.getAftermathMap().get(getUniqueIdentifier()).get(0)); + this.module = (T) API.getRandomAftermathModule(getUniqueIdentifier()) + .orElseGet(() -> API.getAftermathMap().get(getUniqueIdentifier()).get(0)); bindTrackers(); } @@ -71,7 +70,6 @@ protected void addTracker(ITracker tracker) { @Override public List getTrackers() { - TRACKERS.forEach(tracker -> tracker.setUUID(uuid)); return TRACKERS; } @@ -120,7 +118,7 @@ public void deserializeNBT(CompoundTag nbt) { this.progressPercent = nbt.getFloat(Constant.PROGRESS); CompoundTag moduleTag = nbt.getCompound(Constant.MODULE); - IAftermathModule aftermathModule = AFTERMATH_API.getAftermathModule(this.getUniqueIdentifier()); + IAftermathModule aftermathModule = API.getAftermathModule(this.getUniqueIdentifier()); aftermathModule.deserializeNBT(moduleTag); this.module = (T) aftermathModule; diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseAftermathModule.java b/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseAftermathModule.java index 5a7de49..155155d 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseAftermathModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseAftermathModule.java @@ -63,9 +63,11 @@ protected void setRewards(ItemWeightedListModule rewards) { Rewards = rewards; } + @Override public String getJsonName() { return jsonName == null ? getUniqueIdentifier().toLowerCase() : jsonName.toLowerCase(); } + @Override public void setJsonName(String jsonName) { this.jsonName = jsonName; } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseTracker.java b/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseTracker.java index 8c4bf23..e0f87fd 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseTracker.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/base/BaseTracker.java @@ -11,8 +11,9 @@ public abstract class BaseTracker implements ITracker { protected UUID uuid; @Override - public void setUUID(UUID uuid) { + public ITracker setUUID(UUID uuid) { this.uuid = uuid; + return this; } @Override diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/module/IAftermathModule.java b/src/main/java/com/pancake/surviving_the_aftermath/api/module/IAftermathModule.java index 002b9d3..026484e 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/module/IAftermathModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/module/IAftermathModule.java @@ -9,4 +9,8 @@ public interface IAftermathModule extends IIdentifier, IJSONSerializable, INBTSerializable { SimpleWeightedRandomList getRewardList(); + + String getJsonName(); + + void setJsonName(String jsonName); } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/data/pack/AftermathModuleLoader.java b/src/main/java/com/pancake/surviving_the_aftermath/common/data/pack/AftermathModuleLoader.java index 936f68b..d392e2e 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/data/pack/AftermathModuleLoader.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/data/pack/AftermathModuleLoader.java @@ -9,6 +9,7 @@ import com.pancake.surviving_the_aftermath.api.Constant; import com.pancake.surviving_the_aftermath.api.aftermath.AftermathAPI; import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import com.pancake.surviving_the_aftermath.common.util.AftermathEventUtil; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener; @@ -39,6 +40,7 @@ protected void apply(Map jsonElementMap, @NotNull IAftermathModule aftermathModule = instance.getAftermathModule(identifier); try { aftermathModule.deserializeJson(asJsonObject); + aftermathModule.setJsonName(resourceLocation.getPath()); if (!AFTERMATH_MODULE_MAP.containsKey(identifier)) { AFTERMATH_MODULE_MAP.put(identifier, new ArrayList<>(List.of(aftermathModule))); } @@ -47,6 +49,8 @@ protected void apply(Map jsonElementMap, @NotNull SurvivingTheAftermath.LOGGER.error("Failed to deserialize aftermath module: " + aftermathModule.getUniqueIdentifier()); } }); + + instance.finishAftermathMap(AFTERMATH_MODULE_MAP); } } \ No newline at end of file diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/NetherRaid.java b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/NetherRaid.java index 7a577c7..ef0cabf 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/NetherRaid.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/NetherRaid.java @@ -9,7 +9,6 @@ import com.pancake.surviving_the_aftermath.api.IAftermathFactory; import com.pancake.surviving_the_aftermath.api.base.BaseAftermathModule; import com.pancake.surviving_the_aftermath.api.module.IEntityInfoModule; -import com.pancake.surviving_the_aftermath.common.event.AftermathEvent; import com.pancake.surviving_the_aftermath.common.init.ModStructures; import com.pancake.surviving_the_aftermath.common.raid.api.BaseRaid; import com.pancake.surviving_the_aftermath.common.raid.module.NetherRaidModule; @@ -35,7 +34,6 @@ import net.minecraft.world.entity.ai.memory.MemoryModuleType; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Mirror; @@ -309,8 +307,9 @@ protected void setSpawnPos(PortalShape portalShape){ @Override public void bindTrackers() { super.bindTrackers(); - addTracker(new RaidMobBattleTracker()); - addTracker(new RaidPlayerBattleTracker()); + API.getTracker(uuid, RaidMobBattleTracker.IDENTIFIER, + RaidPlayerBattleTracker.IDENTIFIER) + .forEach(this::addTracker); } public static class Factory implements IAftermathFactory { diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/BaseRaid.java b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/BaseRaid.java index 8697b4a..440c6a9 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/BaseRaid.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/BaseRaid.java @@ -9,6 +9,8 @@ import com.pancake.surviving_the_aftermath.api.module.IEntityInfoModule; import com.pancake.surviving_the_aftermath.common.raid.module.BaseRaidModule; import com.pancake.surviving_the_aftermath.common.tracker.MobBattleTracker; +import com.pancake.surviving_the_aftermath.common.tracker.RaidMobBattleTracker; +import com.pancake.surviving_the_aftermath.common.tracker.RaidPlayerBattleTracker; import com.pancake.surviving_the_aftermath.common.util.RandomUtils; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; @@ -38,12 +40,13 @@ public BaseRaid(ServerLevel level) { @Override public void bindTrackers() { - addTracker(new MobBattleTracker()); + API.getTracker(uuid, MobBattleTracker.IDENTIFIER) + .forEach(this::addTracker); } @Override public boolean isCreate() { - Map> aftermathMap = AFTERMATH_MANAGER.getAftermathMap(); + Map> aftermathMap = MANAGER.getAftermathMap(); return aftermathMap.values().stream() .filter(aftermath -> aftermath instanceof IRaid) .map(aftermath -> (IRaid) aftermath) diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/util/AftermathEventUtil.java b/src/main/java/com/pancake/surviving_the_aftermath/common/util/AftermathEventUtil.java index 1f1de9e..b4c196e 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/util/AftermathEventUtil.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/util/AftermathEventUtil.java @@ -2,9 +2,11 @@ import com.pancake.surviving_the_aftermath.api.AftermathState; import com.pancake.surviving_the_aftermath.api.base.BaseAftermath; +import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; import com.pancake.surviving_the_aftermath.common.event.AftermathEvent; import com.pancake.surviving_the_aftermath.compat.kubejs.event.AftermathEventJS; import com.pancake.surviving_the_aftermath.compat.kubejs.event.AftermathEvents; +import com.pancake.surviving_the_aftermath.compat.kubejs.event.AftermathModifyEventJS; import dev.latvian.mods.kubejs.event.EventResult; import net.minecraft.server.level.ServerLevel; import net.minecraft.util.random.SimpleWeightedRandomList; @@ -62,4 +64,8 @@ public static void end(BaseAftermath aftermath, Set players, ServerLevel l MinecraftForge.EVENT_BUS.post(new AftermathEvent.End(aftermath,players, level)); AftermathEvents.END.post(new AftermathEventJS.EndJS(aftermath, players, level)); } + + public static void modify(String identifier, List aftermaths) { + EventResult result = AftermathEvents.MODIFY.post(new AftermathModifyEventJS(identifier, aftermaths)); + } } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/ModExtra.java b/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/ModExtra.java new file mode 100644 index 0000000..259d39f --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/ModExtra.java @@ -0,0 +1,24 @@ +package com.pancake.surviving_the_aftermath.compat.kubejs; + +import com.pancake.surviving_the_aftermath.api.aftermath.AftermathAPI; +import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import dev.latvian.mods.kubejs.event.Extra; + +import java.util.List; + +public class ModExtra{ + protected static final AftermathAPI API = AftermathAPI.getInstance(); + + public static final Extra AFTERMATH_TYPE = new Extra().transformer(ModExtra::toRegistryAftermath); + + + private static List toRegistryAftermath(Object object){ + if (object == null) { + return null; + } + if (object instanceof String str) { + return API.getAftermathModules(str); + } + return null; + } +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/event/AftermathEvents.java b/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/event/AftermathEvents.java index d2daafe..486711a 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/event/AftermathEvents.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/event/AftermathEvents.java @@ -1,5 +1,6 @@ package com.pancake.surviving_the_aftermath.compat.kubejs.event; +import com.pancake.surviving_the_aftermath.compat.kubejs.ModExtra; import dev.latvian.mods.kubejs.event.EventGroup; import dev.latvian.mods.kubejs.event.EventHandler; @@ -27,6 +28,8 @@ public interface AftermathEvents { //Celebrating EventHandler CELEBRATING = GROUP.server("celebrating", () -> AftermathEventJS.CelebratingJS.class); + EventHandler MODIFY = GROUP.startup("modify", () -> AftermathModifyEventJS.class).extra(ModExtra.AFTERMATH_TYPE); + static void register() { GROUP.register(); } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/event/AftermathModifyEventJS.java b/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/event/AftermathModifyEventJS.java new file mode 100644 index 0000000..b36f000 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/compat/kubejs/event/AftermathModifyEventJS.java @@ -0,0 +1,32 @@ +package com.pancake.surviving_the_aftermath.compat.kubejs.event; + +import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import dev.latvian.mods.kubejs.event.EventJS; + +import java.util.List; + +public class AftermathModifyEventJS extends EventJS { + private final String identifier; + private final List aftermathModules; + + public AftermathModifyEventJS(String identifier, List aftermathModules) { + this.identifier = identifier; + this.aftermathModules = aftermathModules; + } + + public String getIdentifier() { + return identifier; + } + + public List getAftermathModules() { + return aftermathModules; + } + + public void remove(String JsonName) { + aftermathModules.removeIf(aftermathModule -> aftermathModule.getJsonName().equals(JsonName)); + } + + public void add(IAftermathModule aftermathModule) { + aftermathModules.add(aftermathModule); + } +}