Skip to content

Commit

Permalink
fixed ruleSet getter
Browse files Browse the repository at this point in the history
  • Loading branch information
rotgruengelb committed Mar 30, 2024
1 parent 8e44727 commit ca12d6c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.rotgruengelb.landscape.feature.zones.rule.RuleSetDataManager;
import net.rotgruengelb.landscape.feature.zones.rule.RuleSet;
import net.rotgruengelb.landscape.feature.zones.rule.RuleSetDataManager;

import java.util.Map;
import java.util.Set;

public class RuleSetsDebuglet {
public static int showAvailableRuleSets(CommandContext<ServerCommandSource> ctx) {
Set<Identifier> rulesets = RuleSetDataManager.getRuleSets();
for (Identifier ruleSet : rulesets) {
Map<Identifier, RuleSet> ruleSets = RuleSetDataManager.getRuleSets();
for (Identifier identifier : ruleSets.keySet()) {
ctx.getSource()
.sendMessage(Text.literal("ruleset: " + ruleSet + " -> " + RuleSetDataManager.getRuleSet(ruleSet)
.sendMessage(Text.literal("ruleset: " + identifier + " -> " + ruleSets.get(identifier)
.name()));
}
return 1;
}

public static int showRuleSetRules(CommandContext<ServerCommandSource> ctx) {
String ruleSetName = StringArgumentType.getString(ctx, "ruleset_name");
RuleSet ruleSet = RuleSetDataManager.getRuleSet(Identifier.tryParse(ruleSetName));
RuleSet ruleSet = RuleSetDataManager.getRuleSet(Identifier.tryParse(ruleSetName), ctx.getSource()
.getWorld());
if (ruleSet == null) {
ctx.getSource()
.sendError(Text.literal("No RuleSet with name " + ruleSetName + " found"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static Optional<Boolean> allowsAction(BlockPos pos, String rule, World wo
zoneManagerContexts.stream()
.filter(zoneManagerContext -> zoneManagerContext.isBlockPosInZone(pos, false))
.forEach(zoneManagerContext -> {
RuleSet ruleSet = RuleSet.of(zoneManagerContext.ruleSet());
RuleSet ruleSet = RuleSet.of(zoneManagerContext.ruleSet(), world);
if (ruleSet.containsRule(rule)) {
values.put(zoneManagerContext.priority(), ruleSet.getRuleValue(rule));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -17,8 +18,8 @@ public record RuleSet(String name,
.forGetter(RuleSet::name), Codec.unboundedMap(Codec.STRING, Codec.BOOL)
.fieldOf("rules").forGetter(RuleSet::rules)).apply(instance, RuleSet::new));

public static RuleSet of(Identifier name) {
return RuleSetDataManager.getRuleSet(name);
public static RuleSet of(Identifier name, World world) {
return RuleSetDataManager.getRuleSet(name, world);
}

public boolean containsRule(String rule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.World;
import net.rotgruengelb.landscape.Landscape;
import net.rotgruengelb.landscape.registry.LandscapeRegistries;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class RuleSetDataManager extends JsonDataLoader implements IdentifiableResourceReloadListener {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping()
.create();
private static final Map<Identifier, RuleSet> ruleSets = new HashMap<>();
public static RuleSetDataManager INSTANCE;
private final DynamicRegistryManager registryManager;
private final Logger LOGGER = Landscape.getClassLogger();
Expand All @@ -37,13 +38,12 @@ public static void register(DynamicRegistryManager registryManager) {
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(INSTANCE);
}

public static RuleSet getRuleSet(Identifier identifier) {
return INSTANCE.registryManager.get(LandscapeRegistries.RULESETS).getOrEmpty(identifier)
.orElse(RuleSet.EMPTY_RULESET);
public static RuleSet getRuleSet(Identifier identifier, World world) {
return world.getRegistryManager().get(LandscapeRegistries.RULESETS).get(identifier);
}

public static Set<Identifier> getRuleSets() {
return INSTANCE.registryManager.get(LandscapeRegistries.RULESETS).getIds();
public static Map<Identifier, RuleSet> getRuleSets() {
return ruleSets;
}

@Override
Expand All @@ -54,6 +54,8 @@ public Identifier getFabricId() {
@Override
protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager manager, Profiler profiler) {

ruleSets.clear();

LOGGER.info("Loading Data RuleSets for Landscape! ( Path: data/{namespace}/landscapes/rulesets/{name}.json -> RuleSet: {namespace}:{name} )");

for (Identifier identifier : prepared.keySet()) {
Expand All @@ -70,7 +72,7 @@ protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager mana
rules.put(entry.getKey(), entry.getValue().getAsBoolean());
}

RuleSet ruleSet = new RuleSet(json.get("name").getAsString(), rules);
ruleSets.put(identifier, new RuleSet(json.get("name").getAsString(), rules));

LOGGER.debug("Loaded RuleSet: " + identifier);
}
Expand Down

This file was deleted.

0 comments on commit ca12d6c

Please sign in to comment.