Skip to content

Commit 48de10c

Browse files
committed
Remove unnecessary structure registry and make providers provide structures directly
Signed-off-by: HellFirePvP <7419378+HellFirePvP@users.noreply.github.com>
1 parent 13b3d69 commit 48de10c

7 files changed

Lines changed: 25 additions & 100 deletions

File tree

src/main/java/hellfirepvp/observerlib/api/structure/MatchableStructure.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,4 @@ default public boolean matchesSingleBlock(@Nullable BlockGetter reader,
105105
(tileEntity == null || (tileMatch == null || tileMatch.matches(reader, center.offset(centerOffset), tileEntity)));
106106
}
107107
}
108-
109-
public ResourceLocation getRegistryName();
110108
}

src/main/java/hellfirepvp/observerlib/api/util/PatternBlockArray.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package hellfirepvp.observerlib.api.util;
22

3-
import hellfirepvp.observerlib.api.structure.PlaceableStructure;
4-
import net.minecraft.resources.ResourceLocation;
3+
import hellfirepvp.observerlib.api.structure.MatchableStructure;
54

65
/**
6+
* This class is an exemplary simple implementation of the matchable structure interface.
7+
* Can be used to observe structure integrity with observers.
8+
*
79
* This class is part of the ObserverLib Mod
810
* The complete source code for this mod can be found on github.
911
* Class: StructureBlockArray
1012
* Created by HellFirePvP
11-
* Date: 11.08.2019 / 09:38
13+
* Date: 11.08.2019 / 09:12
1214
*/
13-
public class StructureBlockArray extends PatternBlockArray implements PlaceableStructure {
14-
15-
public StructureBlockArray(ResourceLocation registryName) {
16-
super(registryName);
17-
}
18-
15+
public class StructureBlockArray extends BlockArray implements MatchableStructure {
1916
}

src/main/java/hellfirepvp/observerlib/common/CommonProxy.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import hellfirepvp.observerlib.common.event.BlockChangeNotifier;
1010
import hellfirepvp.observerlib.common.event.handler.EventHandlerIO;
1111
import hellfirepvp.observerlib.common.registry.RegistryProviders;
12-
import hellfirepvp.observerlib.common.registry.RegistryStructures;
1312
import net.neoforged.bus.api.IEventBus;
1413
import net.neoforged.neoforge.event.server.ServerStartedEvent;
1514
import net.neoforged.neoforge.event.server.ServerStoppingEvent;
@@ -43,7 +42,6 @@ private void registerBlocks(IEventBus modEventBus) {
4342

4443
private void registerRegistries(NewRegistryEvent event) {
4544
RegistryProviders.initialize(event);
46-
RegistryStructures.initialize(event);
4745
}
4846

4947
public void attachEventHandlers(IEventBus eventBus) {

src/main/java/hellfirepvp/observerlib/common/change/ObserverProviderStructure.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
import com.mojang.serialization.Codec;
44
import com.mojang.serialization.MapCodec;
55
import com.mojang.serialization.codecs.RecordCodecBuilder;
6-
import hellfirepvp.observerlib.api.ChangeObserver;
76
import hellfirepvp.observerlib.api.structure.MatchableStructure;
87
import hellfirepvp.observerlib.api.ObserverProvider;
98
import hellfirepvp.observerlib.common.registry.RegistryProviders;
10-
import hellfirepvp.observerlib.common.registry.RegistryStructures;
9+
import hellfirepvp.observerlib.common.util.CodecUtil;
1110
import net.minecraft.core.BlockPos;
12-
import net.minecraft.resources.ResourceLocation;
13-
import org.jetbrains.annotations.NotNull;
1411

1512
import javax.annotation.Nonnull;
16-
import java.util.ArrayList;
1713
import java.util.function.Function;
1814

1915
/**
@@ -25,19 +21,21 @@
2521
*/
2622
public class ObserverProviderStructure extends ObserverProvider<ChangeObserverStructure> {
2723

24+
public static final Codec<ObserverProviderStructure> PROVIDER_CODEC = RegistryProviders.getRegistry().byNameCodec()
25+
.xmap(provider -> CodecUtil.informedCast(provider, ObserverProviderStructure.class), Function.identity());
2826
public static final MapCodec<ChangeObserverStructure> OBSERVER_CODEC = RecordCodecBuilder.mapCodec(builder -> builder.group(
29-
ResourceLocation.CODEC.fieldOf("structure").forGetter(observer -> observer.getProvider().getStructureName()),
27+
PROVIDER_CODEC.fieldOf("provider").forGetter(ChangeObserverStructure::getProvider),
3028
BlockPos.CODEC.listOf().fieldOf("mismatches").forGetter(ChangeObserverStructure::getMismatches)
31-
).apply(builder, (structure, mismatches) -> new ObserverProviderStructure(structure).newObserver().addMismatches(mismatches)));
29+
).apply(builder, (provider, mismatches) -> provider.newObserver().addMismatches(mismatches)));
3230

33-
private final ResourceLocation structureName;
31+
private final MatchableStructure structure;
3432

35-
public ObserverProviderStructure(ResourceLocation structureName) {
36-
this.structureName = structureName;
33+
public ObserverProviderStructure(MatchableStructure structure) {
34+
this.structure = structure;
3735
}
3836

39-
public ResourceLocation getStructureName() {
40-
return this.structureName;
37+
public MatchableStructure getStructure() {
38+
return structure;
4139
}
4240

4341
@Override
@@ -48,10 +46,6 @@ public MapCodec<ChangeObserverStructure> codec() {
4846
@Override
4947
@Nonnull
5048
public ChangeObserverStructure newObserver() {
51-
MatchableStructure structure = RegistryStructures.getStructure(this.getStructureName());
52-
if (structure == null) {
53-
throw new IllegalStateException("Tried creating structure observer for unknown structure: " + this.getStructureName());
54-
}
55-
return new ChangeObserverStructure(this, structure);
49+
return new ChangeObserverStructure(this, this.getStructure());
5650
}
5751
}

src/main/java/hellfirepvp/observerlib/common/registry/RegistryStructures.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main/java/hellfirepvp/observerlib/common/util/CodecUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public static <T> Codec<T> registryNameCodec(Registry<T> registry) {
4242
public static <T> T cast(Object obj) {
4343
return (T) obj;
4444
}
45+
46+
public static <T> T informedCast(Object obj, Class<T> target) {
47+
try {
48+
return (T) obj;
49+
} catch (ClassCastException e) {
50+
throw new RuntimeException("Failed to cast " + obj + " to " + target, e);
51+
}
52+
}
4553
}

0 commit comments

Comments
 (0)