diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/AftermathManager.java b/src/main/java/com/pancake/surviving_the_aftermath/api/AftermathManager.java new file mode 100644 index 0000000..1a2b8c8 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/AftermathManager.java @@ -0,0 +1,66 @@ +package com.pancake.surviving_the_aftermath.api; + +import com.google.common.collect.Maps; +import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; +import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtOps; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.Level; + +import java.util.Iterator; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +public class AftermathManager { + private final Map> AFTERMATH_MAP = Maps.newHashMap(); + private static final AftermathManager INSTANCE = new AftermathManager(); + public static AftermathManager getInstance() { return INSTANCE; } + private AftermathManager() {} + + public void tick() { + Iterator> iterator = AFTERMATH_MAP.values().iterator(); + while (iterator.hasNext()) { + IAftermath raid = iterator.next(); +// if (raid.isEnd() || raid.isLose()) { +// iterator.remove(); // 通过迭代器安全地移除元素 +// } else { +// raid.tick(); +// } + } + } + + private void remove(IAftermath aftermath) { + AFTERMATH_MAP.remove(aftermath.getUUID()); + } + + private void add(IAftermath aftermath) { + AFTERMATH_MAP.put(aftermath.getUUID(), aftermath); + } + + public Map> getAftermathMap() { + return AFTERMATH_MAP; + } + + public Optional> getAftermath(UUID uuid) { + return Optional.ofNullable(AFTERMATH_MAP.get(uuid)); + } + + public boolean create(IAftermath aftermath) { +// if (aftermath.isCreate()) { +// add(aftermath); +// return true; +// } +// return false; + } + + public void create(Level level, CompoundTag compoundTag) { +// IAftermath.CODEC.get().parse(NbtOps.INSTANCE,compoundTag) +// .resultOrPartial(SurvivingTheAftermath.LOGGER::error) +// .ifPresent(aftermath -> { +// aftermath.setServerLevel(level); +// add(aftermath); +// }); + } +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/AftermathState.java b/src/main/java/com/pancake/surviving_the_aftermath/api/AftermathState.java new file mode 100644 index 0000000..f2761e3 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/AftermathState.java @@ -0,0 +1,20 @@ +package com.pancake.surviving_the_aftermath.api; + +public enum AftermathState { + START("start", 0), + READY("ready", 1), + ONGOING("ongoing", 2), + VICTORY("victory", 3), + LOSE("lose", 4), + CELEBRATING("celebrating", 5), + END("end", 6); + + private final String name; + private final int index; + + AftermathState(String name, int index) { + this.name = name; + this.index = index; + } + +} \ No newline at end of file diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/IAftermath.java b/src/main/java/com/pancake/surviving_the_aftermath/api/IAftermath.java index c6aeb09..c57253a 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/IAftermath.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/IAftermath.java @@ -1,6 +1,43 @@ package com.pancake.surviving_the_aftermath.api; +import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.api.base.BaseAftermathModule; import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import com.pancake.surviving_the_aftermath.common.init.ModuleRegistry; +import net.minecraft.core.BlockPos; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; + +import java.util.Set; +import java.util.UUID; +import java.util.function.Predicate; +import java.util.function.Supplier; public interface IAftermath extends IModule> { + Supplier>> CODEC = () -> ModuleRegistry.AFTERMATH_REGISTRY.get().getCodec() + .dispatch("aftermath", IAftermath::type, IAftermath::codec); + boolean isCreate(Level level, BlockPos pos,Player player); + void tick(); + boolean isEnd(); + boolean isLose(); + void updatePlayers(); + void updateProgress(); + void spawnRewards(); + Predicate validPlayer(); + + ResourceLocation getRegistryName(); + AftermathState getState(); + Level getLevel(); + Set getPlayers(); + Set getEnemies(); + BaseAftermathModule getModule(); + UUID getUUID(); + float getProgressPercent(); + + + + void insertTag(LivingEntity entity); } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/IIdentifier.java b/src/main/java/com/pancake/surviving_the_aftermath/api/IIdentifier.java deleted file mode 100644 index ab14efe..0000000 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/IIdentifier.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.pancake.surviving_the_aftermath.api; - -public interface IIdentifier { - String getUniqueIdentifier(); -} \ No newline at end of file diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/IModule.java b/src/main/java/com/pancake/surviving_the_aftermath/api/IModule.java index b23f9f8..3732a54 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/api/IModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/IModule.java @@ -1,4 +1,4 @@ package com.pancake.surviving_the_aftermath.api; -public interface IModule extends IIdentifier,ICodec { +public interface IModule extends ICodec { } 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 910609a..f8d32f8 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 @@ -1,7 +1,185 @@ package com.pancake.surviving_the_aftermath.api.base; +import com.google.common.collect.Sets; +import com.pancake.surviving_the_aftermath.api.AftermathManager; +import com.pancake.surviving_the_aftermath.api.AftermathState; import com.pancake.surviving_the_aftermath.api.IAftermath; +import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import com.pancake.surviving_the_aftermath.common.data.pack.AftermathModuleLoader; +import com.pancake.surviving_the_aftermath.common.module.condition.StructureConditionModule; +import com.pancake.surviving_the_aftermath.util.RegistryUtil; +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.Registries; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerBossEvent; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.BossEvent; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.StructureManager; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.properties.StructureMode; +import net.minecraft.world.level.levelgen.structure.Structure; +import net.minecraft.world.level.levelgen.structure.StructurePiece; +import net.minecraft.world.level.levelgen.structure.StructureStart; +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate; +import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager; + +import java.util.*; +import java.util.function.Predicate; public abstract class BaseAftermath implements IAftermath { + protected final AftermathManager MANAGER = AftermathManager.getInstance(); + protected AftermathState state; + protected Level level; + protected Set players = Sets.newHashSet(); + protected Set enemies = Sets.newHashSet(); + protected T module; + protected final ServerBossEvent progress = new ServerBossEvent(Component.empty(), BossEvent.BossBarColor.RED, BossEvent.BossBarOverlay.PROGRESS); + protected final UUID uuid = progress.getId(); + protected float progressPercent = progress.getProgress(); + + + public BaseAftermath(AftermathState state, Set players, Set enemies, T module, float progressPercent) { + this.state = state; + this.players = players; + this.enemies = enemies; + this.module = module; + this.progressPercent = progressPercent; + } + public BaseAftermath(Level level, BlockPos pos, Player player){ + this.level = level; + this.module = (T) getRandomAftermathModule(); + getSpawnPos(level,pos); + } + + + public BaseAftermath() { + } + + public IAftermathModule getRandomAftermathModule() { + Collection modules = AftermathModuleLoader.AFTERMATH_MODULE_MAP.get(getRegistryName()); + return modules.stream().findAny().get(); + } + + public void getSpawnPos(Level level, BlockPos pos) { + if (level instanceof ServerLevel serverLevel){ + Optional module = getModule().getConditions().stream() + .filter(condition -> condition instanceof StructureConditionModule structureModule) + .findFirst(); + module.ifPresent(structureModule ->{ + if (structureModule instanceof StructureConditionModule structureConditionModule){ + if (structureConditionModule.checkCondition(level,pos)) { + String moduleStructure = structureConditionModule.getStructure(); + ResourceKey key = RegistryUtil.keyStructure(moduleStructure); + Structure structure = level.registryAccess().registryOrThrow(Registries.STRUCTURE).get(key); + + StructureManager structureManager1 = serverLevel.structureManager(); + if (structure != null) { + StructureStart structureAt = structureManager1.getStructureAt(pos, structure); + ChunkPos chunkPos = structureAt.getChunkPos(); + List pieces = structureAt.getPieces(); + for (StructurePiece piece : pieces) { + BlockPos realPos = piece.getLocatorPosition().offset(chunkPos.x * 16, 0, chunkPos.z * 16); // 计算真实的世界坐标 + System.out.println("realPos : " + realPos + "BlockState : " + serverLevel.getBlockState(realPos).getBlock()); + } + } + } + } + }); + } + } + + @Override + public boolean isCreate(Level level, BlockPos pos, Player player) { + return getModule().isCreate(level, pos,player); + } + + @Override + public void tick() { + if (isEnd()) return; + + updateProgress(); + if (state == AftermathState.VICTORY){ + this.progressPercent = 0; + spawnRewards(); + } + } + + @Override + public void updateProgress() { + progress.setProgress(progressPercent); + updatePlayers(); + } + + @Override + public void updatePlayers() { + if (level instanceof ServerLevel serverLevel){ + final Set oldPlayers = Sets.newHashSet(progress.getPlayers()); + final Set newPlayers = Sets.newHashSet(serverLevel.getPlayers(this.validPlayer())); + players.clear(); + newPlayers.stream() + .filter(player -> !oldPlayers.contains(player)) + .forEach(progress::addPlayer); + oldPlayers.stream() + .filter(player -> !newPlayers.contains(player)) + .forEach(progress::removePlayer); + progress.getPlayers().forEach(player -> players.add(player.getUUID())); + } + } + + @Override + public Predicate validPlayer() { + return (Predicate) player -> !player.isSpectator(); + } + @Override + public void spawnRewards() { + + } + + + + + @Override + public boolean isEnd() { + return this.state == AftermathState.END; + } + @Override + public boolean isLose() { + return this.state == AftermathState.LOSE; + } + @Override + public AftermathState getState() { + return state; + } + @Override + public Level getLevel() { + return level; + } + @Override + public Set getPlayers() { + return players; + } + @Override + public Set getEnemies() { + return enemies; + } + @Override + public T getModule() { + return module; + } + @Override + public UUID getUUID() { + return uuid; + } + @Override + public float getProgressPercent() { + return progressPercent; + } } \ No newline at end of file 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 d274152..be795f0 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 @@ -1,42 +1,83 @@ package com.pancake.surviving_the_aftermath.api.base; -import com.mojang.serialization.Codec; -import com.mojang.serialization.codecs.RecordCodecBuilder; import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import com.pancake.surviving_the_aftermath.common.module.condition.LevelConditionModule; +import com.pancake.surviving_the_aftermath.common.module.condition.PlayerConditionModule; +import com.pancake.surviving_the_aftermath.common.module.condition.StructureConditionModule; import com.pancake.surviving_the_aftermath.common.module.weighted.ItemWeightedModule; -import com.pancake.surviving_the_aftermath.common.raid.module.BaseRaidModule; +import net.minecraft.core.BlockPos; import net.minecraft.util.random.WeightedEntry; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; +import net.minecraft.world.level.Level; import java.util.List; -import java.util.function.Function; +import java.util.Optional; public abstract class BaseAftermathModule implements IAftermathModule { - protected ItemWeightedModule Rewards; + protected ItemWeightedModule rewards; + protected List conditions; protected String jsonName; - public BaseAftermathModule(ItemWeightedModule rewards) { - Rewards = rewards; + public BaseAftermathModule(ItemWeightedModule rewards, List conditions) { + this.rewards = rewards; + this.conditions = getFilterConditions(conditions); } public BaseAftermathModule() { } + + //过滤条件 + public List getFilterConditions(List conditions){ + Optional module = conditions.stream() + .filter(condition -> condition instanceof StructureConditionModule) + .findFirst(); + + module.ifPresent(iConditionModule -> + conditions.removeIf(condition -> condition instanceof StructureConditionModule && condition != iConditionModule)); + + return conditions; + } + + @Override + public boolean isCreate(Level level, BlockPos pos, Player player) { + conditions.forEach(condition -> { + if(condition instanceof LevelConditionModule levelConditionModule){ + levelConditionModule.checkCondition(level,pos); + } + if (condition instanceof PlayerConditionModule playerConditionModule){ + playerConditionModule.checkCondition(player); + } + }); + return true; + } + + public List getConditions() { + return conditions; + } + public ItemWeightedModule getRewards() { - return Rewards; + return rewards; } public List> getRewardsList() { - return Rewards.getList(); + return rewards.getList(); } public BaseAftermathModule setRewards(ItemWeightedModule rewards) { - Rewards = rewards; + this.rewards = rewards; + return this; + } + + public BaseAftermathModule setConditions(List conditions) { + this.conditions = conditions; return this; } @Override public String getJsonName() { - return jsonName == null ? getUniqueIdentifier().toLowerCase() : jsonName.toLowerCase(); + return jsonName; } @Override public void setJsonName(String jsonName) { 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 3218a88..b9f7be1 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 @@ -4,6 +4,9 @@ import com.mojang.serialization.Codec; import com.pancake.surviving_the_aftermath.api.IModule; import com.pancake.surviving_the_aftermath.common.init.ModuleRegistry; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; import java.util.function.Supplier; @@ -13,4 +16,6 @@ public interface IAftermathModule extends IModule { String getJsonName(); void setJsonName(String jsonName); + + boolean isCreate(Level level,BlockPos pos, Player player); } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/capability/AftermathCap.java b/src/main/java/com/pancake/surviving_the_aftermath/common/capability/AftermathCap.java new file mode 100644 index 0000000..67dcec4 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/capability/AftermathCap.java @@ -0,0 +1,76 @@ +package com.pancake.surviving_the_aftermath.common.capability; + +import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; +import com.pancake.surviving_the_aftermath.api.AftermathManager; +import com.pancake.surviving_the_aftermath.api.IAftermath; +import com.pancake.surviving_the_aftermath.common.init.ModCapability; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtOps; +import net.minecraft.world.level.Level; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ICapabilitySerializable; +import net.minecraftforge.common.util.INBTSerializable; +import net.minecraftforge.common.util.LazyOptional; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class AftermathCap implements INBTSerializable { + private static final AftermathManager AFTERMATH_MANAGER = AftermathManager.getInstance(); + private final Level level; + + public AftermathCap(Level level) { this.level = level;} + + @Override + public CompoundTag serializeNBT() { + CompoundTag compoundTag = new CompoundTag(); + AFTERMATH_MANAGER.getAftermathMap().forEach((uuid, aftermath) -> { + IAftermath.CODEC.get().encodeStart(NbtOps.INSTANCE, aftermath) + .resultOrPartial(SurvivingTheAftermath.LOGGER::error) + .ifPresent(tag -> { + compoundTag.put(uuid.toString(), tag); + }); + }); + return compoundTag; + } + + @Override + public void deserializeNBT(CompoundTag compoundTag) { + for (String uuid : compoundTag.getAllKeys()) { + CompoundTag tag = compoundTag.getCompound(uuid); + AFTERMATH_MANAGER.create(level, tag); + } + } + + public static LazyOptional get(Level level) { + return level.getCapability(ModCapability.AFTERMATH_CAP); + } + + public void tick() { + AFTERMATH_MANAGER.tick(); + } + + public static class Provider implements ICapabilitySerializable { + private final LazyOptional instance; + + public Provider(Level level) { + instance = LazyOptional.of(() -> new AftermathCap(level)); + } + + + @Override + public @NotNull LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { + return ModCapability.AFTERMATH_CAP.orEmpty(cap, instance); + } + + @Override + public CompoundTag serializeNBT() { + return instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional cannot be empty!")).serializeNBT(); + } + + @Override + public void deserializeNBT(CompoundTag nbt) { + instance.orElseThrow(() -> new IllegalArgumentException("LazyOptional cannot be empty!")).deserializeNBT(nbt); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/data/datagen/raid/NetherRaidModuleProvider.java b/src/main/java/com/pancake/surviving_the_aftermath/common/data/datagen/raid/NetherRaidModuleProvider.java index b32d1d7..c351983 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/data/datagen/raid/NetherRaidModuleProvider.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/data/datagen/raid/NetherRaidModuleProvider.java @@ -3,6 +3,7 @@ import com.pancake.surviving_the_aftermath.common.data.datagen.AftermathModuleProviders; import com.pancake.surviving_the_aftermath.common.module.amount.IntegerAmountModule; import com.pancake.surviving_the_aftermath.common.module.amount.RandomAmountModule; +import com.pancake.surviving_the_aftermath.common.module.condition.StructureConditionModule; import com.pancake.surviving_the_aftermath.common.module.entity_info.EntityInfoModule; import com.pancake.surviving_the_aftermath.common.module.entity_info.EntityInfoWithEquipmentModule; import com.pancake.surviving_the_aftermath.common.module.weighted.ItemWeightedModule; @@ -34,7 +35,9 @@ public void addModules() { netherRaidModule.setReadyTime(10).setWaves(List.of( List.of(entityInfoModule), List.of(equipmentModule) - )).setRewards(itemWeightedModule); + )).setRewards(itemWeightedModule).setConditions(List.of( + new StructureConditionModule("surviving_the_aftermath:") + )); addModule(netherRaidModule); } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/event/PlayerEvent.java b/src/main/java/com/pancake/surviving_the_aftermath/common/event/PlayerEvent.java index 2819382..49cae62 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/event/PlayerEvent.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/event/PlayerEvent.java @@ -15,6 +15,7 @@ import com.pancake.surviving_the_aftermath.common.module.amount.RandomAmountModule; import com.pancake.surviving_the_aftermath.common.module.entity_info.EntityInfoModule; import com.pancake.surviving_the_aftermath.common.module.weighted.ItemWeightedModule; +import com.pancake.surviving_the_aftermath.common.raid.BaseRaid; import com.pancake.surviving_the_aftermath.common.raid.module.NetherRaidModule; import net.minecraft.nbt.NbtOps; import net.minecraft.nbt.Tag; @@ -39,88 +40,7 @@ public static void onPlayerInteractRightClickBlock(PlayerInteractEvent.RightClic if (level.isClientSide() || hand != InteractionHand.MAIN_HAND) { return; } - -// List>> entityList = Arrays.asList( -// WeightedEntry.wrap(EntityType.CREEPER, 10), -// WeightedEntry.wrap(EntityType.ZOMBIE, 20) -// ); -// - -// -// EntityTypeWeightedListModule module = new EntityTypeWeightedListModule(); -// - -// module.Codec.encodeStart(opsNBT, entityList).result().ifPresent(nbt -> { -// DataResult>>> parse = module.Codec.parse(opsNBT, nbt); -// parse.result().ifPresent(list -> { -// System.out.println("opsNBT List: " + list); -// }); -// System.out.println("opsNBT nbt" + nbt); -// }); -// Object o1 = module.transformedCodec.encodeStart(opsJSON, entityList).result().get(); -// System.out.println("opsNBT: " + o); -// System.out.println("opsJSON: " + o1); -// -// module.Codec.encodeStart(opsJSON, entityList).result().ifPresent(jsonElement -> { -// DataResult>>> parse = module.Codec.parse(opsJSON, jsonElement); -// parse.result().ifPresent(list -> { -// System.out.println("opsJSON List: " + list); -// }); -// System.out.println("opsJSON jsonElement: " + jsonElement); -// }); -// -// module.transformedCodec.encodeStart(opsJSON, entityList).result().ifPresent(json -> { -// DataResult>>> result = module.transformedCodec.parse(opsNBT, json); -// result.result().ifPresent(list -> { -// System.out.println("opsJSON List: " + list); -// }); -// }); - - - RandomAmountModule randomAmountModule = new RandomAmountModule(1, 5); - - EntityInfoModule entityInfoModule = new EntityInfoModule(EntityType.PIG, randomAmountModule); - - DynamicOps opsNBT = NbtOps.INSTANCE; - DynamicOps opsJSON = JsonOps.INSTANCE; - - -// NetherRaidModule netherRaidModule = new NetherRaidModule(); -// ItemWeightedModule itemWeightedModule = new ItemWeightedModule(List.of( -// WeightedEntry.wrap(Items.STONE, 10), -// WeightedEntry.wrap(Items.STONE_AXE, 20) -// )); -// netherRaidModule.setReadyTime(10).setWaves(List.of( -// List.of(entityInfoModule), -// List.of(entityInfoModule) -// )).setRewards(itemWeightedModule) -// .setJsonName("nether_raid"); -// - AftermathModuleLoader.AFTERMATH_MODULE_MAP.forEach((identifier, module) -> { - System.out.println("identifier: " + identifier); - System.out.println("module: " + module); - IAftermathModule.CODEC.get().encodeStart(opsNBT, module).result().ifPresent(nbt -> { - DataResult parse = IAftermathModule.CODEC.get().parse(opsNBT, nbt); - parse.result().ifPresent(module1 -> { - System.out.println("opsNBT module: " + module1); - System.out.println(module1.type()); - }); - System.out.println("opsNBT nbt" + nbt); - }); - }); - - - -// ModAftermathModule.MODULE_CODEC.get().encodeStart(opsNBT, randomAmountModule).result().ifPresent(nbt -> { -// DataResult parse = ModAftermathModule.MODULE_CODEC.get().parse(opsNBT, nbt); -// parse.result().ifPresent(module -> { -// System.out.println("opsNBT module: " + module); -// }); -// System.out.println("opsNBT nbt" + nbt); -// }); - - - - + new BaseRaid<>(level, event.getPos(), event.getEntity()); } + } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModAftermathModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModAftermathModule.java index a402b65..02434d7 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModAftermathModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModAftermathModule.java @@ -3,7 +3,7 @@ import com.pancake.surviving_the_aftermath.api.module.*; import com.pancake.surviving_the_aftermath.common.module.amount.IntegerAmountModule; import com.pancake.surviving_the_aftermath.common.module.amount.RandomAmountModule; -import com.pancake.surviving_the_aftermath.common.module.condition.StageConditionModule; +import com.pancake.surviving_the_aftermath.common.module.condition.*; import com.pancake.surviving_the_aftermath.common.module.entity_info.EntityInfoModule; import com.pancake.surviving_the_aftermath.common.module.entity_info.EntityInfoWithEquipmentModule; import com.pancake.surviving_the_aftermath.common.module.weighted.EntityTypeWeightedModule; @@ -29,10 +29,12 @@ public class ModAftermathModule { public static final RegistryObject> ITEM_WEIGHTED = ModuleRegistry.WEIGHTED_MODULE.register(ItemWeightedModule.IDENTIFIER, ItemWeightedModule::new); - public static final RegistryObject STAGE_CONDITION = ModuleRegistry.CONDITION_MODULE.register("stage", StageConditionModule::new); - - public static final RegistryObject NETHER_RAID = ModuleRegistry.AFTERMATH_MODULE.register(NetherRaid.IDENTIFIER, NetherRaidModule::new); + public static final RegistryObject LEVEL_CONDITION = ModuleRegistry.CONDITION_MODULE.register(StructureConditionModule.IDENTIFIER, StructureConditionModule::new); + public static final RegistryObject BIOMES_CONDITION = ModuleRegistry.CONDITION_MODULE.register(BiomesConditionModule.IDENTIFIER, BiomesConditionModule::new); + public static final RegistryObject Y_AXIS_HEIGHT_CONDITION = ModuleRegistry.CONDITION_MODULE.register(YAxisHeightConditionModule.IDENTIFIER, YAxisHeightConditionModule::new); + public static final RegistryObject WEATHER_CONDITION = ModuleRegistry.CONDITION_MODULE.register(WeatherConditionModule.IDENTIFIER, WeatherConditionModule::new); + public static final RegistryObject XP_CONDITION = ModuleRegistry.CONDITION_MODULE.register(XpConditionModule.IDENTIFIER, XpConditionModule::new); } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModCapability.java b/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModCapability.java new file mode 100644 index 0000000..6163566 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModCapability.java @@ -0,0 +1,35 @@ +package com.pancake.surviving_the_aftermath.common.init; + +import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; +import com.pancake.surviving_the_aftermath.common.capability.AftermathCap; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +import net.minecraftforge.common.capabilities.*; +import net.minecraftforge.event.AttachCapabilitiesEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber +public class ModCapability{ + public static final Capability AFTERMATH_CAP = CapabilityManager.get(new CapabilityToken<>() {}); + @SubscribeEvent + public static void registerCapabilities(RegisterCapabilitiesEvent event) { + event.register(AftermathCap.class); + } + + @SubscribeEvent + public static void attachLevelCapability(AttachCapabilitiesEvent event) { + if (event.getObject().dimension() == Level.OVERWORLD) { + event.addCapability(SurvivingTheAftermath.asResource("aftermath_cap"), new AftermathCap.Provider(event.getObject())); + } + } + + @SubscribeEvent + public static void attachEntityCapability(AttachCapabilitiesEvent event) { + if (event.getObject() instanceof Player player) { + + } + } + +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModuleRegistry.java b/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModuleRegistry.java index 05dca2b..677a46e 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModuleRegistry.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/init/ModuleRegistry.java @@ -2,6 +2,7 @@ import com.mojang.serialization.Codec; import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; +import com.pancake.surviving_the_aftermath.api.IAftermath; import com.pancake.surviving_the_aftermath.api.module.*; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; @@ -13,6 +14,8 @@ import java.util.function.Supplier; public class ModuleRegistry { + public static final DeferredRegister> AFTERMATH = DeferredRegister.create(Keys.AFTERMATH, SurvivingTheAftermath.MOD_ID); + public static Supplier>> AFTERMATH_REGISTRY = AFTERMATH.makeRegistry(RegistryBuilder::new); public static final DeferredRegister AFTERMATH_MODULE = DeferredRegister.create(Keys.AFTERMATH_MODULE, SurvivingTheAftermath.MOD_ID); public static Supplier> AFTERMATH_MODULE_REGISTRY = AFTERMATH_MODULE.makeRegistry(RegistryBuilder::new); @@ -30,6 +33,7 @@ public class ModuleRegistry { public static Supplier> CONDITION_REGISTRY = CONDITION_MODULE.makeRegistry(RegistryBuilder::new); public static void register(IEventBus bus) { + AFTERMATH.register(bus); AFTERMATH_MODULE.register(bus); AMOUNT_MODULE.register(bus); ENTITY_INFO_MODULE.register(bus); @@ -39,6 +43,7 @@ public static void register(IEventBus bus) { public static final class Keys { + public static final ResourceKey>> AFTERMATH = key("aftermath"); public static final ResourceKey> AFTERMATH_MODULE = key("aftermath_module"); public static final ResourceKey> AMOUNT = key("amount"); public static final ResourceKey> ENTITY_INFO = key("entity_info"); diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/amount/IntegerAmountModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/amount/IntegerAmountModule.java index 13146f7..4943d19 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/module/amount/IntegerAmountModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/amount/IntegerAmountModule.java @@ -18,12 +18,6 @@ public IntegerAmountModule(int amount) { public IntegerAmountModule() { } - - @Override - public String getUniqueIdentifier() { - return IDENTIFIER; - } - @Override public Codec codec() { return CODEC; diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/amount/RandomAmountModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/amount/RandomAmountModule.java index be6be8f..e3d54a0 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/module/amount/RandomAmountModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/amount/RandomAmountModule.java @@ -33,13 +33,6 @@ public RandomAmountModule(int min, int max) { public RandomAmountModule() { } - - @Override - public String getUniqueIdentifier() { - return IDENTIFIER; - } - - @Override public int getSpawnAmount() { this.max = Math.max(this.max, this.min); diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/BiomesConditionModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/BiomesConditionModule.java new file mode 100644 index 0000000..be7201a --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/BiomesConditionModule.java @@ -0,0 +1,43 @@ +package com.pancake.surviving_the_aftermath.common.module.condition; + +import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import com.pancake.surviving_the_aftermath.common.init.ModAftermathModule; +import net.minecraft.core.BlockPos; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.biome.Biome; +import net.minecraftforge.registries.ForgeRegistries; + +public class BiomesConditionModule extends LevelConditionModule{ + public static final String IDENTIFIER = "biomes_condition"; + public static final Codec CODEC = Codec.STRING.xmap(BiomesConditionModule::new, BiomesConditionModule::getBiomes); + public String biomes; + + public BiomesConditionModule(String biomes) { + this.biomes = biomes; + } + + public BiomesConditionModule() { + } + + public String getBiomes() { + return biomes; + } + + @Override + public boolean checkCondition(Level level, BlockPos pos) { + Biome biome = ForgeRegistries.BIOMES.getValue(ResourceLocation.tryParse(biomes)); + return level.getBiome(pos).get() == biome; + } + + @Override + public Codec codec() { + return CODEC; + } + + @Override + public IConditionModule type() { + return ModAftermathModule.BIOMES_CONDITION.get(); + } +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/LevelConditionModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/LevelConditionModule.java new file mode 100644 index 0000000..410ade2 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/LevelConditionModule.java @@ -0,0 +1,16 @@ +package com.pancake.surviving_the_aftermath.common.module.condition; + +import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import net.minecraft.core.BlockPos; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; + +public abstract class LevelConditionModule implements IConditionModule { + public LevelConditionModule() { + } + + public boolean checkCondition(Level level, BlockPos pos) { + return true; + } +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/PlayerConditionModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/PlayerConditionModule.java new file mode 100644 index 0000000..de4c1e0 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/PlayerConditionModule.java @@ -0,0 +1,15 @@ +package com.pancake.surviving_the_aftermath.common.module.condition; + +import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import net.minecraft.world.entity.player.Player; + +public abstract class PlayerConditionModule implements IConditionModule { + public PlayerConditionModule() { + } + + public boolean checkCondition(Player player) { + return true; + } + +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/StageConditionModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/StageConditionModule.java deleted file mode 100644 index 50d6dff..0000000 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/StageConditionModule.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.pancake.surviving_the_aftermath.common.module.condition; - -import com.mojang.serialization.Codec; -import com.pancake.surviving_the_aftermath.api.module.IConditionModule; - -public class StageConditionModule implements IConditionModule { - public static final String IDENTIFIER = "stage_condition"; - public static final Codec CODEC = Codec.STRING.xmap(StageConditionModule::new, StageConditionModule::getStage); - public String stage; - - public StageConditionModule(String stage) { - this.stage = stage; - } - - public StageConditionModule() { - } - @Override - public Codec codec() { - return null; - } - - @Override - public IConditionModule type() { - return null; - } - - @Override - public String getUniqueIdentifier() { - return IDENTIFIER; - } - - public String getStage() { - return stage; - } - -} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/StructureConditionModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/StructureConditionModule.java new file mode 100644 index 0000000..40633dd --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/StructureConditionModule.java @@ -0,0 +1,55 @@ +package com.pancake.surviving_the_aftermath.common.module.condition; + +import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import com.pancake.surviving_the_aftermath.common.init.ModAftermathModule; +import com.pancake.surviving_the_aftermath.util.RegistryUtil; +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.levelgen.structure.Structure; +import net.minecraftforge.registries.ForgeRegistries; + +import java.util.Objects; + +public class StructureConditionModule extends LevelConditionModule{ + public static final String IDENTIFIER = "structure_condition"; + public static final Codec CODEC = Codec.STRING.xmap(StructureConditionModule::new, StructureConditionModule::getStructure); + public String structure; + + public StructureConditionModule(String structure) { + this.structure = structure; + } + + public StructureConditionModule() { + } + + @Override + public boolean checkCondition(Level level, BlockPos pos) { + if (level instanceof ServerLevel serverLevel){ + ResourceKey key = RegistryUtil.keyStructure(structure); + return serverLevel.structureManager().getAllStructuresAt(pos) + .containsKey(level.registryAccess().registryOrThrow(Registries.STRUCTURE).get(key)); + } + return false; + } + + + @Override + public Codec codec() { + return CODEC; + } + + @Override + public IConditionModule type() { + return ModAftermathModule.LEVEL_CONDITION.get(); + } + + public String getStructure() { + return structure; + } +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/WeatherConditionModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/WeatherConditionModule.java new file mode 100644 index 0000000..d9c3851 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/WeatherConditionModule.java @@ -0,0 +1,40 @@ +package com.pancake.surviving_the_aftermath.common.module.condition; + +import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import com.pancake.surviving_the_aftermath.common.init.ModAftermathModule; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; + +public class WeatherConditionModule extends LevelConditionModule{ + public static final String IDENTIFIER = "weather_condition"; + public static final Codec CODEC = Codec.STRING.xmap(WeatherConditionModule::new, WeatherConditionModule::getWeather); + public String weather; + + public WeatherConditionModule(String weather) { + this.weather = weather; + } + public WeatherConditionModule() { + } + + @Override + public boolean checkCondition(Level level, BlockPos pos) { + if (level.isRaining()) { + return weather.equals("rain"); + } else if (level.isThundering()) { + return weather.equals("thunder"); + } else { + return weather.equals("clear"); + } + } + + @Override + public Codec codec() { + return CODEC; + } + + @Override + public IConditionModule type() { + return ModAftermathModule.WEATHER_CONDITION.get(); + } +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/XpConditionModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/XpConditionModule.java new file mode 100644 index 0000000..34dcfd1 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/XpConditionModule.java @@ -0,0 +1,33 @@ +package com.pancake.surviving_the_aftermath.common.module.condition; + +import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import com.pancake.surviving_the_aftermath.common.init.ModAftermathModule; +import net.minecraft.world.entity.player.Player; + +public class XpConditionModule extends PlayerConditionModule{ + public static final String IDENTIFIER = "xp_condition"; + public static final Codec CODEC = Codec.INT.fieldOf("xp").xmap(XpConditionModule::new, XpConditionModule::xp).codec(); + public int xp; + + public XpConditionModule(int xp) { + this.xp = xp; + } + public XpConditionModule() { + } + + @Override + public boolean checkCondition(Player player) { + return player.experienceLevel >= xp; + } + + @Override + public Codec codec() { + return CODEC; + } + + @Override + public IConditionModule type() { + return ModAftermathModule.XP_CONDITION.get(); + } +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/YAxisHeightConditionModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/YAxisHeightConditionModule.java new file mode 100644 index 0000000..9899a08 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/condition/YAxisHeightConditionModule.java @@ -0,0 +1,56 @@ +package com.pancake.surviving_the_aftermath.common.module.condition; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; +import com.pancake.surviving_the_aftermath.common.init.ModAftermathModule; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; + +public class YAxisHeightConditionModule extends LevelConditionModule{ + public static final String IDENTIFIER = "y_axis_height_condition"; + public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.INT.fieldOf("y_axis_height").forGetter(YAxisHeightConditionModule::getYAxisHeight), + Codec.INT.fieldOf("flag").forGetter(YAxisHeightConditionModule::getFlag) + ).apply(instance, YAxisHeightConditionModule::new)); + public int yAxisHeight; + public int flag = 0; + + public YAxisHeightConditionModule(int yAxisHeight, int flag) { + this.yAxisHeight = yAxisHeight; + this.flag = flag; + } + + public YAxisHeightConditionModule() { + } + + @Override + public boolean checkCondition(Level level, BlockPos pos) { + if (flag == 0) { + return pos.getY() == yAxisHeight; + } else if (flag == 1) { + return pos.getY() < yAxisHeight; + } else if (flag == 2) { + return pos.getY() > yAxisHeight; + } + return false; + } + + public int getYAxisHeight() { + return yAxisHeight; + } + + public int getFlag() { + return flag; + } + + @Override + public Codec codec() { + return CODEC; + } + + @Override + public IConditionModule type() { + return ModAftermathModule.Y_AXIS_HEIGHT_CONDITION.get(); + } +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/entity_info/EntityInfoModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/entity_info/EntityInfoModule.java index 6839713..81669c4 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/module/entity_info/EntityInfoModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/entity_info/EntityInfoModule.java @@ -26,12 +26,6 @@ public EntityInfoModule(EntityType entityType, IAmountModule amountModule) { public EntityInfoModule() { } - - @Override - public String getUniqueIdentifier() { - return IDENTIFIER; - } - public EntityType getEntityType() { return entityType; } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/entity_info/EntityInfoWithEquipmentModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/entity_info/EntityInfoWithEquipmentModule.java index 96e0dfa..b8f50a1 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/module/entity_info/EntityInfoWithEquipmentModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/entity_info/EntityInfoWithEquipmentModule.java @@ -35,12 +35,6 @@ public EntityInfoWithEquipmentModule(EntityType entityType, IAmountModule amo public EntityInfoWithEquipmentModule() { } - - @Override - public String getUniqueIdentifier() { - return IDENTIFIER; - } - @Override public Codec codec() { return CODEC; diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/weighted/EntityTypeWeightedModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/weighted/EntityTypeWeightedModule.java index 9e78204..262d49e 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/module/weighted/EntityTypeWeightedModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/weighted/EntityTypeWeightedModule.java @@ -27,12 +27,6 @@ public EntityTypeWeightedModule(List>> list) public EntityTypeWeightedModule() { } - - @Override - public String getUniqueIdentifier() { - return IDENTIFIER; - } - @Override public Codec>> codec() { return CODEC; diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/module/weighted/ItemWeightedModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/module/weighted/ItemWeightedModule.java index b7dd859..e9cc60c 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/module/weighted/ItemWeightedModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/module/weighted/ItemWeightedModule.java @@ -26,11 +26,6 @@ public ItemWeightedModule(List> list) { public ItemWeightedModule() { } - @Override - public String getUniqueIdentifier() { - return IDENTIFIER; - } - @Override public Codec> codec() { return CODEC; diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/BaseRaid.java b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/BaseRaid.java index 9bf320e..5409ec4 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/BaseRaid.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/BaseRaid.java @@ -1,9 +1,119 @@ package com.pancake.surviving_the_aftermath.common.raid; +import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; +import com.pancake.surviving_the_aftermath.api.AftermathState; +import com.pancake.surviving_the_aftermath.api.IAftermath; import com.pancake.surviving_the_aftermath.api.base.BaseAftermath; +import com.pancake.surviving_the_aftermath.api.base.BaseAftermathModule; +import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import com.pancake.surviving_the_aftermath.api.module.IEntityInfoModule; import com.pancake.surviving_the_aftermath.common.raid.api.IRaid; import com.pancake.surviving_the_aftermath.common.raid.module.BaseRaidModule; +import com.pancake.surviving_the_aftermath.util.RandomUtils; +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.StringTag; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +import net.minecraftforge.common.util.LazyOptional; -public abstract class BaseRaid extends BaseAftermath implements IRaid { +import java.util.*; +public class BaseRaid extends BaseAftermath implements IRaid { + public static final String IDENTIFIER = "raid"; + protected int currentWave = -1; + protected int totalEnemy = 0; + protected BlockPos centerPos; + + public BaseRaid(AftermathState state, Set players, Set enemies, BaseRaidModule module, float progressPercent, int currentWave, int totalEnemy, BlockPos centerPos) { + super(state, players, enemies, module, progressPercent); + this.currentWave = currentWave; + this.totalEnemy = totalEnemy; + this.centerPos = centerPos; + } + public BaseRaid(Level level, BlockPos pos, Player player){ + super(level,pos,player); + } + + public BaseRaid() { + + } + + @Override + public void tick() { + super.tick(); +// if (state == AftermathState.CELEBRATING){ +// if (rewardTime <= 0){ +// end(); +// return; +// } +// spawnRewards(); +// rewardTime--; +// } + } + + @Override + public boolean isCreate(Level level, BlockPos pos, Player player) { + boolean create = super.isCreate(level, pos, player); + Map> aftermathMap = MANAGER.getAftermathMap(); + boolean noneMatch = aftermathMap.values().stream() + .filter(aftermath -> aftermath instanceof IRaid) + .map(aftermath -> (IRaid) aftermath) + .noneMatch(raid -> raid.getCenterPos().distSqr(centerPos) < Math.pow(raid.getRadius(), 2)); + return create && noneMatch; + } + + @Override + public ResourceLocation getRegistryName() { + return SurvivingTheAftermath.asResource(IDENTIFIER); + } + + @Override + public void insertTag(LivingEntity entity) { + entity.getPersistentData().put(IDENTIFIER, StringTag.valueOf("enemies")); + } + + public Player randomPlayersUnderAttack(){ + return level.getPlayerByUUID(RandomUtils.getRandomElement(getPlayers())); + } + public boolean join(Entity entity) { + if (state == AftermathState.ONGOING && + Math.sqrt(entity.blockPosition().distSqr(centerPos)) < getRadius() && + enemies.add(entity.getUUID())) { + totalEnemy++; + return true; + } + return false; + } + + public int getCurrentWave() { + return currentWave; + } + + public int getTotalEnemy() { + return totalEnemy; + } + + @Override + public BlockPos getCenterPos() { + return centerPos; + } + + @Override + public int getRadius() { + return 50; + } + + @Override + public Codec> codec() { + return null; + } + + @Override + public IAftermath type() { + return null; + } } \ 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 1ecca02..8fd7acf 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 @@ -1,12 +1,16 @@ package com.pancake.surviving_the_aftermath.common.raid; import com.mojang.serialization.Codec; +import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; import com.pancake.surviving_the_aftermath.api.IAftermath; import com.pancake.surviving_the_aftermath.api.base.BaseAftermathModule; import com.pancake.surviving_the_aftermath.common.raid.module.NetherRaidModule; import net.minecraft.core.BlockPos; +import net.minecraft.nbt.StringTag; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.LivingEntity; -public class NetherRaid extends BaseRaid { +public class NetherRaid extends BaseRaid { public static final String IDENTIFIER = "nether_raid"; @Override @@ -20,12 +24,12 @@ public IAftermath type() { } @Override - public String getUniqueIdentifier() { - return null; + public ResourceLocation getRegistryName() { + return SurvivingTheAftermath.asResource(IDENTIFIER); } @Override - public BlockPos getCenterPos() { - return null; + public void insertTag(LivingEntity entity) { + entity.getPersistentData().put(IDENTIFIER, StringTag.valueOf("enemies")); } } \ No newline at end of file diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/IRaid.java b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/IRaid.java index 1a28b6d..1b40466 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/IRaid.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/IRaid.java @@ -4,4 +4,5 @@ public interface IRaid { BlockPos getCenterPos(); + int getRadius(); } \ No newline at end of file diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/module/BaseRaidModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/module/BaseRaidModule.java index 5616c67..dc190c8 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/module/BaseRaidModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/module/BaseRaidModule.java @@ -5,6 +5,7 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import com.pancake.surviving_the_aftermath.api.base.BaseAftermathModule; import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; import com.pancake.surviving_the_aftermath.api.module.IEntityInfoModule; import com.pancake.surviving_the_aftermath.common.init.ModuleRegistry; import com.pancake.surviving_the_aftermath.common.module.weighted.ItemWeightedModule; @@ -17,8 +18,8 @@ public abstract class BaseRaidModule extends BaseAftermathModule implements IRaidModule { protected List> waves ; - public BaseRaidModule(ItemWeightedModule rewards, List> waves) { - super(rewards); + public BaseRaidModule(ItemWeightedModule rewards, List conditions, List> waves) { + super(rewards, conditions); this.waves = waves; } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/module/NetherRaidModule.java b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/module/NetherRaidModule.java index 407b04f..e00a849 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/module/NetherRaidModule.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/module/NetherRaidModule.java @@ -4,6 +4,7 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import com.pancake.surviving_the_aftermath.api.module.IAftermathModule; +import com.pancake.surviving_the_aftermath.api.module.IConditionModule; import com.pancake.surviving_the_aftermath.api.module.IEntityInfoModule; import com.pancake.surviving_the_aftermath.common.init.ModAftermathModule; import com.pancake.surviving_the_aftermath.common.init.ModuleRegistry; @@ -15,13 +16,14 @@ public class NetherRaidModule extends BaseRaidModule { public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( ItemWeightedModule.CODEC.fieldOf("rewards").forGetter(NetherRaidModule::getRewards), + Codec.list(IConditionModule.CODEC.get()).fieldOf("conditions").forGetter(NetherRaidModule::getConditions), Codec.list(Codec.list(IEntityInfoModule.CODEC.get())).fieldOf("waves").forGetter(NetherRaidModule::getWaves), Codec.INT.fieldOf("ready_time").forGetter(NetherRaidModule::getReadyTime) ).apply(instance, NetherRaidModule::new)); private int readyTime; - public NetherRaidModule(ItemWeightedModule rewards, List> waves, int readyTime) { - super(rewards, waves); + public NetherRaidModule(ItemWeightedModule rewards, List conditions, List> waves, int readyTime) { + super(rewards, conditions, waves); this.readyTime = readyTime; } @@ -38,11 +40,6 @@ public IAftermathModule type() { return ModAftermathModule.NETHER_RAID.get(); } - @Override - public String getUniqueIdentifier() { - return NetherRaid.IDENTIFIER; - } - public int getReadyTime() { return readyTime; } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/util/RandomUtils.java b/src/main/java/com/pancake/surviving_the_aftermath/util/RandomUtils.java new file mode 100644 index 0000000..638d4bd --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/util/RandomUtils.java @@ -0,0 +1,34 @@ +package com.pancake.surviving_the_aftermath.util; + +import java.util.Collection; +import java.util.List; +import java.util.Random; + +public class RandomUtils { + private static final Random RANDOM = new Random(); + public static T getRandomElement(Collection collection) { + int size = collection.size(); + if (size == 0) { + throw new IllegalArgumentException("Collection cannot be empty."); + } + int randomIndex = RANDOM.nextInt(size); + if (collection instanceof List) { + return ((List) collection).get(randomIndex); + } else { + int i = 0; + for (T item : collection) { + if (i == randomIndex) { + return item; + } + i++; + } + throw new IllegalStateException("Unexpected condition: randomIndex matched no element."); + } + } + + public static boolean randomChanceOf(double percent) { + return RANDOM.nextDouble() <= percent; + } + + +} \ No newline at end of file diff --git a/src/main/java/com/pancake/surviving_the_aftermath/util/RegistryUtil.java b/src/main/java/com/pancake/surviving_the_aftermath/util/RegistryUtil.java new file mode 100644 index 0000000..068765b --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/util/RegistryUtil.java @@ -0,0 +1,67 @@ +package com.pancake.surviving_the_aftermath.util; + +import com.mojang.logging.LogUtils; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.item.Item; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.levelgen.structure.Structure; +import net.minecraftforge.registries.ForgeRegistries; +import net.minecraftforge.registries.RegistryObject; +import org.slf4j.Logger; + +import java.util.Objects; + +public class RegistryUtil { + public static final Logger LOGGER = LogUtils.getLogger(); + public static EntityType getEntityTypeFromRegistryName(String registryName) { + EntityType entityType = ForgeRegistries.ENTITY_TYPES.getValue(ResourceLocation.tryParse(registryName)); + if (entityType == null) { + LOGGER.error("Entity with registry name {} does not exist!", registryName); + } + return entityType; + } + public static ResourceLocation getRegistryNameFromEntityType(EntityType entityType) { + return ForgeRegistries.ENTITY_TYPES.getKey(entityType); + } + + + public static Block getBlockFromRegistryName(String registryName) { + Block block = ForgeRegistries.BLOCKS.getValue(ResourceLocation.tryParse(registryName)); + if (block == null) { + LOGGER.error("Block with registry name {} does not exist!", registryName); + } + return block; + } + + public static ResourceLocation getRegistryNameFromBlock(Block block) { + return ForgeRegistries.BLOCKS.getKey(block); + } + + public static Item getItemFromRegistryName(String registryName) { + Item item = ForgeRegistries.ITEMS.getValue(ResourceLocation.tryParse(registryName)); + if (item == null) { + LOGGER.error("Item with registry name {} does not exist!", registryName); + } + return item; + } + + public static ResourceLocation getRegistryNameFromItem(Item item) { + return ForgeRegistries.ITEMS.getKey(item); + } + + public static ResourceKey keyStructure(String name) { + return ResourceKey.create(Registries.STRUCTURE, Objects.requireNonNull(ResourceLocation.tryParse(name))); + } + +// public static Iterable getKnownBlocks() { +// return ModBlocks.BLOCKS.getEntries().stream().map(RegistryObject::get)::iterator; +// } +// +// public static Iterable getKnownItems() { +// return ModItems.ITEMS.getEntries().stream().map(RegistryObject::get)::iterator; +// } + +}