Skip to content

Commit

Permalink
Add AftermathModifyEventJS
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoHuNao committed Nov 7, 2023
1 parent 8bf819c commit 156bd17
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ static void unregister(ITracker tracker) {
MinecraftForge.EVENT_BUS.unregister(tracker);
}

public void setUUID(UUID uuid);
public ITracker setUUID(UUID uuid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<IAftermathModule>> AFTERMATH_MODULE_MAP = Maps.newHashMap();
Expand Down Expand Up @@ -43,15 +41,25 @@ public Optional<IAftermathModule> getRandomAftermathModule(String identifier) {
return Optional.ofNullable(aftermathModules.get(new Random().nextInt(aftermathModules.size())));
}

public List<IAftermathModule> getAftermathModules(String identifier) {
return AFTERMATH_MODULE_MAP.get(identifier);
}


public void registerTracker(String identifier, Class<? extends ITracker> tracker) {
TRACKERS.put(identifier, tracker);
}

public ITracker getTracker(String identifier) {
return getObjectInstance(TRACKERS, identifier);
public List<ITracker> 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<? extends IAmountModule> amountModule) {
AMOUNT_MODULES_MAP.put(identifier, amountModule);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends BaseAftermathModule> implements IAftermath<BaseAftermathModule> {
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<ITracker> TRACKERS = Lists.newArrayList();
protected AftermathState state;
protected ServerLevel level;
Expand All @@ -44,8 +43,8 @@ public abstract class BaseAftermath<T extends BaseAftermathModule> 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();
}

Expand All @@ -71,7 +70,6 @@ protected void addTracker(ITracker tracker) {

@Override
public List<ITracker> getTrackers() {
TRACKERS.forEach(tracker -> tracker.setUUID(uuid));
return TRACKERS;
}

Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@

public interface IAftermathModule extends IIdentifier, IJSONSerializable, INBTSerializable<CompoundTag> {
SimpleWeightedRandomList<Item> getRewardList();

String getJsonName();

void setJsonName(String jsonName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +40,7 @@ protected void apply(Map<ResourceLocation, JsonElement> 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)));
}
Expand All @@ -47,6 +49,8 @@ protected void apply(Map<ResourceLocation, JsonElement> jsonElementMap, @NotNull
SurvivingTheAftermath.LOGGER.error("Failed to deserialize aftermath module: " + aftermathModule.getUniqueIdentifier());
}
});


instance.finishAftermathMap(AFTERMATH_MODULE_MAP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<UUID, IAftermath<BaseAftermathModule>> aftermathMap = AFTERMATH_MANAGER.getAftermathMap();
Map<UUID, IAftermath<BaseAftermathModule>> aftermathMap = MANAGER.getAftermathMap();
return aftermathMap.values().stream()
.filter(aftermath -> aftermath instanceof IRaid)
.map(aftermath -> (IRaid) aftermath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,4 +64,8 @@ public static void end(BaseAftermath aftermath, Set<UUID> 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<IAftermathModule> aftermaths) {
EventResult result = AftermathEvents.MODIFY.post(new AftermathModifyEventJS(identifier, aftermaths));
}
}
Original file line number Diff line number Diff line change
@@ -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<IAftermathModule> toRegistryAftermath(Object object){
if (object == null) {
return null;
}
if (object instanceof String str) {
return API.getAftermathModules(str);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IAftermathModule> aftermathModules;

public AftermathModifyEventJS(String identifier, List<IAftermathModule> aftermathModules) {
this.identifier = identifier;
this.aftermathModules = aftermathModules;
}

public String getIdentifier() {
return identifier;
}

public List<IAftermathModule> getAftermathModules() {
return aftermathModules;
}

public void remove(String JsonName) {
aftermathModules.removeIf(aftermathModule -> aftermathModule.getJsonName().equals(JsonName));
}

public void add(IAftermathModule aftermathModule) {
aftermathModules.add(aftermathModule);
}
}

0 comments on commit 156bd17

Please sign in to comment.