Skip to content

Commit f7ee5ec

Browse files
authored
Merge pull request #74 from MIA-Development-Team/feat/abyss-trial-spawner
feat: abyss trial spawner
2 parents 9001bae + 4adf84f commit f7ee5ec

19 files changed

+1117
-43
lines changed

src/generated/resources/assets/mia/blockstates/abyss_spawner.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"model": "mia:block/abyss_spawner_active"
1717
},
1818
"ominous=false,trial_spawner_state=waiting_for_reward_ejection": {
19-
"model": "mia:block/abyss_spawner_active"
19+
"model": "mia:block/abyss_spawner_ejecting_reward"
2020
},
2121
"ominous=true,trial_spawner_state=active": {
2222
"model": "mia:block/abyss_spawner_active_ominous"
@@ -34,7 +34,7 @@
3434
"model": "mia:block/abyss_spawner_active_ominous"
3535
},
3636
"ominous=true,trial_spawner_state=waiting_for_reward_ejection": {
37-
"model": "mia:block/abyss_spawner_active_ominous"
37+
"model": "mia:block/abyss_spawner_ejecting_reward_ominous"
3838
}
3939
}
4040
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"base_mobs": 1,
3+
"entity_tables": [
4+
{
5+
"entity": "minecraft:warden",
6+
"weight": 1
7+
}
8+
],
9+
"loot_tables": [
10+
{
11+
"loot_table": "minecraft:chests/trial_chambers/reward_unique",
12+
"weight": 10
13+
},
14+
{
15+
"loot_table": "minecraft:chests/trial_chambers/reward_ominous_rare",
16+
"weight": 5
17+
}
18+
],
19+
"mobs_per_player": 0,
20+
"spawn_per_tick": 1,
21+
"spawn_range": 8
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"base_mobs": 2,
3+
"entity_tables": [
4+
{
5+
"entity": "minecraft:skeleton",
6+
"weight": 10
7+
},
8+
{
9+
"entity": "minecraft:stray",
10+
"weight": 5
11+
},
12+
{
13+
"entity": "minecraft:bogged",
14+
"weight": 2
15+
}
16+
],
17+
"loot_tables": [
18+
{
19+
"loot_table": "minecraft:chests/trial_chambers/reward_common",
20+
"weight": 10
21+
},
22+
{
23+
"loot_table": "minecraft:chests/trial_chambers/reward_rare",
24+
"weight": 5
25+
}
26+
],
27+
"mobs_per_player": 1,
28+
"spawn_per_tick": 1,
29+
"spawn_range": 5
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"base_mobs": 4,
3+
"entity_tables": [
4+
{
5+
"entity": "minecraft:spider",
6+
"weight": 10
7+
},
8+
{
9+
"entity": "minecraft:cave_spider",
10+
"weight": 5
11+
}
12+
],
13+
"loot_tables": [
14+
{
15+
"loot_table": "minecraft:chests/trial_chambers/reward_common",
16+
"weight": 10
17+
}
18+
],
19+
"mobs_per_player": 2,
20+
"spawn_per_tick": 2,
21+
"spawn_range": 6
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"base_mobs": 3,
3+
"entity_tables": [
4+
{
5+
"entity": "minecraft:zombie",
6+
"weight": 10
7+
},
8+
{
9+
"entity": "minecraft:husk",
10+
"weight": 5
11+
},
12+
{
13+
"entity": "minecraft:drowned",
14+
"weight": 3
15+
}
16+
],
17+
"loot_tables": [
18+
{
19+
"loot_table": "minecraft:chests/trial_chambers/reward_common",
20+
"weight": 10
21+
},
22+
{
23+
"loot_table": "minecraft:chests/trial_chambers/reward_rare",
24+
"weight": 3
25+
},
26+
{
27+
"loot_table": "minecraft:chests/trial_chambers/reward_unique",
28+
"weight": 1
29+
}
30+
],
31+
"mobs_per_player": 2,
32+
"spawn_per_tick": 1,
33+
"spawn_range": 4
34+
}

src/main/java/com/altnoir/mia/MIA.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.altnoir.mia;
22

33
import com.altnoir.mia.core.curse.CurseManager;
4+
import com.altnoir.mia.core.spawner.AbyssTrialSpawnerManager;
5+
import com.altnoir.mia.core.spawner.records.AbyssTrialSpawnerPattern;
46
import com.altnoir.mia.init.*;
57
import com.altnoir.mia.init.event.EventHandle;
68
import com.altnoir.mia.init.worldgen.MiaBiomeSources;
@@ -28,6 +30,7 @@ public class MIA {
2830
public static final Logger LOGGER = LogUtils.getLogger();
2931

3032
public static final CurseManager CURSE_MANAGER = new CurseManager();
33+
public static final AbyssTrialSpawnerManager SPAWNER_MANAGER = new AbyssTrialSpawnerManager();
3134

3235
public MIA(IEventBus modEventBus, ModContainer modContainer) {
3336
modEventBus.addListener(this::commonSetup);
@@ -79,6 +82,7 @@ public void onServerStarting(ServerStartingEvent event) {
7982
@SubscribeEvent
8083
private void reload(final AddReloadListenerEvent event) {
8184
event.addListener(CURSE_MANAGER);
85+
event.addListener(SPAWNER_MANAGER);
8286
}
8387

8488
// You can use EventBusSubscriber to automatically register all static methods

src/main/java/com/altnoir/mia/block/AbyssSpawnerBlock.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, Block
8080
@Override
8181
public void appendHoverText(ItemStack itemStack, Item.TooltipContext context, List<Component> components, TooltipFlag flag) {
8282
super.appendHoverText(itemStack, context, components, flag);
83-
Spawner.appendHoverText(itemStack, components, "spawn_data");
83+
if (itemStack.has(net.minecraft.core.component.DataComponents.BLOCK_ENTITY_DATA)) {
84+
var beData = itemStack.get(net.minecraft.core.component.DataComponents.BLOCK_ENTITY_DATA);
85+
if (beData != null && beData.copyTag().contains("pattern_id")) {
86+
String patternId = beData.copyTag().getString("pattern_id");
87+
components.add(Component.literal("Pattern: " + patternId).withStyle(net.minecraft.ChatFormatting.GRAY));
88+
}
89+
}
8490
}
8591
}

src/main/java/com/altnoir/mia/block/entity/AbyssSpawnerBlockEntity.java

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,46 @@
22

33
import com.altnoir.mia.MIA;
44
import com.altnoir.mia.block.AbyssSpawnerBlock;
5+
import com.altnoir.mia.core.spawner.AbyssTrialSpawner;
6+
import com.altnoir.mia.core.spawner.records.AbyssTrialSpawnerPattern;
57
import com.altnoir.mia.init.MiaBlockEntities;
68
import net.minecraft.core.BlockPos;
79
import net.minecraft.core.HolderLookup;
810
import net.minecraft.nbt.CompoundTag;
9-
import net.minecraft.nbt.NbtOps;
1011
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
11-
import net.minecraft.util.RandomSource;
12-
import net.minecraft.world.entity.EntityType;
12+
import net.minecraft.resources.ResourceLocation;
1313
import net.minecraft.world.level.Level;
14-
import net.minecraft.world.level.Spawner;
1514
import net.minecraft.world.level.block.entity.BlockEntity;
16-
import net.minecraft.world.level.block.entity.trialspawner.PlayerDetector;
17-
import net.minecraft.world.level.block.entity.trialspawner.TrialSpawner;
1815
import net.minecraft.world.level.block.entity.trialspawner.TrialSpawnerState;
1916
import net.minecraft.world.level.block.state.BlockState;
2017
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
18+
import org.jetbrains.annotations.Nullable;
2119

22-
public class AbyssSpawnerBlockEntity extends BlockEntity implements Spawner, TrialSpawner.StateAccessor {
23-
private TrialSpawner abyssSpawner;
20+
public class AbyssSpawnerBlockEntity extends BlockEntity implements AbyssTrialSpawner.StateAccessor {
21+
private final AbyssTrialSpawner abyssSpawner;
22+
@Nullable
23+
private ResourceLocation patternId;
24+
@Nullable
25+
private AbyssTrialSpawnerPattern cachedPattern;
2426

2527
public AbyssSpawnerBlockEntity(BlockPos pos, BlockState state) {
2628
super(MiaBlockEntities.ABYSS_SPAWNER.get(), pos, state);
27-
PlayerDetector playerdetector = PlayerDetector.NO_CREATIVE_PLAYERS;
28-
PlayerDetector.EntitySelector playerdetector$entityselector = PlayerDetector.EntitySelector.SELECT_FROM_LEVEL;
29-
this.abyssSpawner = new TrialSpawner(this, playerdetector, playerdetector$entityselector);
29+
this.abyssSpawner = new AbyssTrialSpawner(this);
3030
}
3131

3232
@Override
3333
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
3434
super.loadAdditional(tag, registries);
35-
if (tag.contains("normal_config")) {
36-
CompoundTag compoundtag = tag.getCompound("normal_config").copy();
37-
tag.put("ominous_config", compoundtag.merge(tag.getCompound("ominous_config")));
38-
}
3935

40-
this.abyssSpawner.codec().parse(NbtOps.INSTANCE, tag).resultOrPartial(MIA.LOGGER::error).ifPresent(p_311911_ -> this.abyssSpawner = p_311911_);
36+
if (tag.contains("pattern_id", CompoundTag.TAG_STRING)) {
37+
this.patternId = ResourceLocation.parse(tag.getString("pattern_id"));
38+
this.refreshPattern();
39+
}
40+
41+
if (tag.contains("abyss_spawner", CompoundTag.TAG_COMPOUND)) {
42+
this.abyssSpawner.load(tag.getCompound("abyss_spawner"));
43+
}
44+
4145
if (this.level != null) {
4246
this.markUpdated();
4347
}
@@ -46,11 +50,12 @@ protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries)
4650
@Override
4751
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
4852
super.saveAdditional(tag, registries);
49-
this.abyssSpawner
50-
.codec()
51-
.encodeStart(NbtOps.INSTANCE, this.abyssSpawner)
52-
.ifSuccess(tag1 -> tag.merge((CompoundTag) tag1))
53-
.ifError(error -> MIA.LOGGER.warn("Failed to encode TrialSpawner {}", error.message()));
53+
54+
if (this.patternId != null) {
55+
tag.putString("pattern_id", this.patternId.toString());
56+
}
57+
58+
tag.put("abyss_spawner", this.abyssSpawner.save(new CompoundTag()));
5459
}
5560

5661
public ClientboundBlockEntityDataPacket getUpdatePacket() {
@@ -59,21 +64,20 @@ public ClientboundBlockEntityDataPacket getUpdatePacket() {
5964

6065
@Override
6166
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
62-
return this.abyssSpawner.getData().getUpdateTag(this.getBlockState().getValue(AbyssSpawnerBlock.STATE));
67+
var tag = new CompoundTag();
68+
if (this.patternId != null) {
69+
tag.putString("pattern_id", this.patternId.toString());
70+
}
71+
tag.put("abyss_spawner", this.abyssSpawner.save(new CompoundTag()));
72+
return tag;
6373
}
6474

6575
@Override
6676
public boolean onlyOpCanSetNbt() {
6777
return true;
6878
}
6979

70-
@Override
71-
public void setEntityId(EntityType<?> entityType, RandomSource random) {
72-
this.abyssSpawner.getData().setEntityId(this.abyssSpawner, random, entityType);
73-
this.setChanged();
74-
}
75-
76-
public TrialSpawner getAbyssSpawner() {
80+
public AbyssTrialSpawner getAbyssSpawner() {
7781
return this.abyssSpawner;
7882
}
7983

@@ -97,4 +101,38 @@ public void markUpdated() {
97101
this.level.sendBlockUpdated(this.worldPosition, this.getBlockState(), this.getBlockState(), 3);
98102
}
99103
}
104+
105+
public void setPatternId(@Nullable ResourceLocation patternId) {
106+
this.patternId = patternId;
107+
this.refreshPattern();
108+
this.setChanged();
109+
}
110+
111+
@Nullable
112+
@Override
113+
public ResourceLocation getPatternId() {
114+
return this.patternId;
115+
}
116+
117+
public void refreshPattern() {
118+
if (this.patternId != null) {
119+
this.cachedPattern = MIA.SPAWNER_MANAGER.getPattern(this.patternId).orElse(null);
120+
if (this.cachedPattern == null) {
121+
MIA.LOGGER.warn("Unknown spawner pattern: {}", this.patternId);
122+
}
123+
} else {
124+
this.cachedPattern = null;
125+
}
126+
}
127+
128+
@Nullable
129+
@Override
130+
public AbyssTrialSpawnerPattern getPattern() {
131+
return this.cachedPattern;
132+
}
133+
134+
@Override
135+
public boolean hasValidPattern() {
136+
return this.cachedPattern != null;
137+
}
100138
}

src/main/java/com/altnoir/mia/core/curse/CurseManager.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.altnoir.mia.core.curse.records.CurseDimension;
44
import com.altnoir.mia.core.curse.records.CurseEffect;
5+
import com.altnoir.mia.util.MiaUtil;
56
import com.google.gson.Gson;
67
import com.google.gson.GsonBuilder;
78
import com.google.gson.JsonElement;
@@ -15,6 +16,7 @@
1516
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
1617
import net.minecraft.util.GsonHelper;
1718
import net.minecraft.util.profiling.ProfilerFiller;
19+
import org.jetbrains.annotations.NotNull;
1820
import org.slf4j.Logger;
1921

2022
import java.io.IOException;
@@ -34,22 +36,18 @@ public CurseManager() {
3436
}
3537

3638
@Override
37-
protected Map<ResourceLocation, JsonElement> prepare(ResourceManager resourceManager, ProfilerFiller profiler) {
39+
protected @NotNull Map<ResourceLocation, JsonElement> prepare(ResourceManager resourceManager, @NotNull ProfilerFiller profiler) {
3840
Map<ResourceLocation, JsonElement> result = new HashMap<>();
3941

4042
for (var namespace : resourceManager.getNamespaces()) {
4143
var basePath = "mia/curse";
4244
var resources = resourceManager.listResources(basePath, loc -> loc.getPath().endsWith(".json"));
4345
for (Map.Entry<ResourceLocation, Resource> entry : resources.entrySet()) {
4446
var fileLoc = entry.getKey();
45-
var path = fileLoc.getPath();
46-
if (!path.startsWith(basePath)) continue;
47-
var trimmedPath = path.substring(basePath.length() + 1);
48-
if (trimmedPath.endsWith(".json")) {
49-
trimmedPath = trimmedPath.substring(0, trimmedPath.length() - 5);
50-
}
51-
var parts = trimmedPath.split("/");
52-
if (parts.length > 2) continue;
47+
var parts = MiaUtil.parseResourcePath(fileLoc.getPath(), basePath);
48+
49+
if (parts == null || parts.length > 2) continue;
50+
5351
var fixedLoc = ResourceLocation.fromNamespaceAndPath(parts[0], parts[1]);
5452
var res = entry.getValue();
5553
try (var stream = res.open()) {
@@ -65,7 +63,7 @@ protected Map<ResourceLocation, JsonElement> prepare(ResourceManager resourceMan
6563
}
6664

6765
@Override
68-
protected void apply(Map<ResourceLocation, JsonElement> resourceLocationJsonElementMap, ResourceManager resourceManager, ProfilerFiller profilerFiller) {
66+
protected void apply(Map<ResourceLocation, JsonElement> resourceLocationJsonElementMap, @NotNull ResourceManager resourceManager, @NotNull ProfilerFiller profilerFiller) {
6967
curseCache.clear();
7068

7169
LOGGER.info("Found {} curse config files.", resourceLocationJsonElementMap.size());

0 commit comments

Comments
 (0)