Skip to content

Commit

Permalink
add ConditionModule
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoHuNao committed Jan 3, 2024
1 parent ef0838a commit a076be8
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.pancake.surviving_the_aftermath.api.aftermath.AftermathAPI;
import com.pancake.surviving_the_aftermath.api.module.impl.amount.FixedAmountModule;
import com.pancake.surviving_the_aftermath.api.module.impl.amount.RandomAmountModule;
import com.pancake.surviving_the_aftermath.api.module.impl.condition.StageConditionModule;
import com.pancake.surviving_the_aftermath.api.module.impl.entity_info.EntityInfoModule;
import com.pancake.surviving_the_aftermath.api.module.impl.entity_info.EntityInfoWithEquipmentModule;
import com.pancake.surviving_the_aftermath.api.module.impl.weighted.EntityTypeWeightedListModule;
Expand Down Expand Up @@ -76,6 +77,9 @@ private void commonSetup(final FMLCommonSetupEvent event) {

instance.registerAftermathModule(NetherRaid.IDENTIFIER, NetherRaidModule.class);
instance.registerAftermathFactory(NetherRaid.IDENTIFIER, NetherRaid.Factory.class);

instance.registerConditionModule(StageConditionModule.LevelStageConditionModule.IDENTIFIER, StageConditionModule.LevelStageConditionModule.class);
instance.registerConditionModule(StageConditionModule.PlayerStageConditionModule.IDENTIFIER, StageConditionModule.PlayerStageConditionModule.class);
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public class Constant {
public static final String SPECTATOR_LIST = "spectatorList";
public static final String TRACKER = "tracker";
public static final String TOTAL_ENEMY = "totalEnemy";
public static final String LEVEL_STAGES = "levelStages";
public static final String PLAYER_STAGES = "playerStages";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.pancake.surviving_the_aftermath.api;

import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.util.INBTSerializable;

import java.util.Collection;

public interface IStageData extends IIdentifier, INBTSerializable<CompoundTag> {
Collection<String> getStages();

boolean hasStage(String stage);

void addStage(String stage);

void removeStage(String stage);

void clear();


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.mojang.logging.LogUtils;
import com.pancake.surviving_the_aftermath.api.IAftermathFactory;
import com.pancake.surviving_the_aftermath.api.ITracker;
import com.pancake.surviving_the_aftermath.api.module.IAftermathModule;
import com.pancake.surviving_the_aftermath.api.module.IAmountModule;
import com.pancake.surviving_the_aftermath.api.module.IEntityInfoModule;
import com.pancake.surviving_the_aftermath.api.module.IWeightedListModule;
import com.pancake.surviving_the_aftermath.api.module.*;
import com.pancake.surviving_the_aftermath.common.util.AftermathEventUtil;
import com.pancake.surviving_the_aftermath.common.util.RandomUtils;
import org.slf4j.Logger;
Expand All @@ -23,6 +20,7 @@ public class AftermathAPI {
private final Map<String, Class<? extends IAftermathFactory>> AFTERMATH_FACTORY_MAP = Maps.newHashMap();
private final Map<String, Class<? extends IAmountModule>> AMOUNT_MODULES_MAP = Maps.newHashMap();
private final Map<String, Class<? extends IEntityInfoModule>> ENTITY_INFO_MODULES_MAP = Maps.newHashMap();
private final Map<String, Class<? extends IConditionModule>> CONDITION_MODULES_MAP = Maps.newHashMap();
private final Map<String, Class<? extends ITracker>> TRACKERS = Maps.newHashMap();
private static final AftermathAPI INSTANCE = new AftermathAPI();
public static final Logger LOGGER = LogUtils.getLogger();
Expand Down Expand Up @@ -62,6 +60,14 @@ public List<ITracker> getTracker(UUID uuid, String... identifiers) {
.toList();
}

public void registerConditionModule(String identifier, Class<? extends IConditionModule> conditionModule) {
CONDITION_MODULES_MAP.put(identifier, conditionModule);
}

public IConditionModule getConditionModule(String identifier) {
return getObjectInstance(CONDITION_MODULES_MAP, identifier);
}



public void registerAmountModule(String identifier, Class<? extends IAmountModule> amountModule) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pancake.surviving_the_aftermath.api.module;

import com.pancake.surviving_the_aftermath.api.IIdentifier;
import com.pancake.surviving_the_aftermath.api.IJSONSerializable;
import com.pancake.surviving_the_aftermath.api.stage.LevelStageData;
import com.pancake.surviving_the_aftermath.api.stage.PlayerStageData;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.util.INBTSerializable;

public interface IConditionModule extends IIdentifier, IJSONSerializable, INBTSerializable<CompoundTag> {
// boolean checkCondition(Player player, PlayerStageData stageData);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.pancake.surviving_the_aftermath.api.module;

import com.google.common.collect.Lists;
import com.pancake.surviving_the_aftermath.api.IIdentifier;
import com.pancake.surviving_the_aftermath.api.IJSONSerializable;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.random.SimpleWeightedRandomList;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.Item;
import net.minecraftforge.common.util.INBTSerializable;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public interface IWeightedListModule<E> extends IJSONSerializable, INBTSerializable<CompoundTag>, IIdentifier {
SimpleWeightedRandomList<E> getWeightedList();
public interface IWeightedListModule<T> extends IJSONSerializable, INBTSerializable<CompoundTag>, IIdentifier {
SimpleWeightedRandomList<T> getWeightedList();

void setWeightedList(Supplier<SimpleWeightedRandomList<E>> weightedList);
void add(E e, int weight);
void remove(E e);
void setWeightedList(Supplier<SimpleWeightedRandomList<T>> weightedList);
void add(T t, int weight);
void remove(T t);

List<T> getList();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.pancake.surviving_the_aftermath.api.module.impl.condition;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.pancake.surviving_the_aftermath.api.Constant;
import com.pancake.surviving_the_aftermath.api.module.IConditionModule;
import com.pancake.surviving_the_aftermath.common.capability.StageDataCap;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;

public abstract class StageConditionModule implements IConditionModule {
protected String stageName;

public StageConditionModule(String stageName) {
this.stageName = stageName;
}

public boolean checkCondition(Level level){
return false;
}

public boolean checkCondition(Player player){
return false;
}


@Override
public CompoundTag serializeNBT() {
CompoundTag compoundTag = new CompoundTag();
compoundTag.putString("stageName", stageName);
return compoundTag;
}

@Override
public void deserializeNBT(CompoundTag nbt) {
this.stageName = nbt.getString("stageName");
}

@Override
public void deserializeJson(JsonElement jsonElement) {
this.stageName = jsonElement.getAsString();
}

@Override
public JsonElement serializeJson() {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("stageName", stageName);
return jsonObject;
}

public static class LevelStageConditionModule extends StageConditionModule {
public static final String IDENTIFIER = Constant.LEVEL_STAGES;
public LevelStageConditionModule(String stageName) {
super(stageName);
}

@Override
public boolean checkCondition(Level level) {
StageDataCap stageDataCap = StageDataCap.get(level).orElse(null);
return stageDataCap.getStageData().hasStage(stageName);
}

@Override
public String getUniqueIdentifier() {
return IDENTIFIER;
}
}

public static class PlayerStageConditionModule extends StageConditionModule {
public static final String IDENTIFIER = Constant.PLAYER_STAGES;
public PlayerStageConditionModule(String stageName) {
super(stageName);
}

@Override
public boolean checkCondition(Player player) {
StageDataCap stageDataCap = StageDataCap.get(player).orElse(null);
return stageDataCap.getStageData().hasStage(stageName);
}

@Override
public String getUniqueIdentifier() {
return IDENTIFIER;
}
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.pancake.surviving_the_aftermath.api.module.impl.weighted;

import com.google.common.collect.Lists;
import com.pancake.surviving_the_aftermath.api.module.IWeightedListModule;
import net.minecraft.util.random.SimpleWeightedRandomList;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public abstract class BaseWeightedListModule<T> implements IWeightedListModule<T> {
Expand Down Expand Up @@ -40,4 +43,10 @@ public void remove(T t) {

this.weightedList = builder.build();
}

public List<T> getList(){
ArrayList<T> list = Lists.newArrayList();
getWeightedList().unwrap().forEach(weightedEntry -> list.add(weightedEntry.getData()));
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.pancake.surviving_the_aftermath.api.stage;

import com.google.common.collect.Sets;
import com.pancake.surviving_the_aftermath.api.Constant;
import com.pancake.surviving_the_aftermath.api.IStageData;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

public class LevelStageData extends StageData {
public LevelStageData() {
super(Constant.LEVEL_STAGES);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.pancake.surviving_the_aftermath.api.stage;

import com.google.common.collect.Sets;
import com.pancake.surviving_the_aftermath.api.Constant;
import com.pancake.surviving_the_aftermath.api.IStageData;

import java.util.Set;

public class PlayerStageData extends StageData{
public PlayerStageData() {
super(Constant.PLAYER_STAGES);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.pancake.surviving_the_aftermath.api.stage;

import com.google.common.collect.Sets;
import com.pancake.surviving_the_aftermath.api.Constant;
import com.pancake.surviving_the_aftermath.api.IStageData;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

public abstract class StageData implements IStageData {
private final Set<String> unlockedStages = Sets.newHashSet();
private final String identifier;

public StageData(String identifier) {
this.identifier = identifier;
}
@Override
public Collection<String> getStages() {

return Collections.unmodifiableCollection(this.unlockedStages);
}

@Override
public boolean hasStage(String stage) {

return this.unlockedStages.contains(stage.toLowerCase());
}

@Override
public void addStage(String stage) {

this.unlockedStages.add(stage.toLowerCase());
}

@Override
public void removeStage(String stage) {

this.unlockedStages.remove(stage.toLowerCase());
}

@Override
public void clear() {

this.unlockedStages.clear();
}


@Override
public String toString () {

return "StageData [unlockedStages=" + this.unlockedStages + "]";
}

@Override
public String getUniqueIdentifier() {
return identifier;
}

@Override
public CompoundTag serializeNBT() {
final CompoundTag tag = new CompoundTag();
final ListTag list = new ListTag();
for (final String stage : this.unlockedStages) {
list.add(StringTag.valueOf(stage));
}
tag.put(identifier, list);
return tag;
}

@Override
public void deserializeNBT(CompoundTag nbt) {
final ListTag list = nbt.getList(identifier, Tag.TAG_STRING);
for (int tagIndex = 0; tagIndex < list.size(); tagIndex++) {
this.addStage(list.getString(tagIndex));
}
}
}
Loading

0 comments on commit a076be8

Please sign in to comment.