Skip to content

Commit

Permalink
RuleSets now synced using DynamicRegistry.
Browse files Browse the repository at this point in the history
Replaced Mod[...] with Landscape[...]
  • Loading branch information
rotgruengelb committed Mar 30, 2024
1 parent 3a34e16 commit 5e8ce88
Show file tree
Hide file tree
Showing 42 changed files with 208 additions and 268 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ archives_base_name = landscape

# Dependencies
fabric_version=0.96.4+1.20.4
nixienaut_version=1.2.0
nixienaut_version=1.3.0
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;
import net.rotgruengelb.landscape.client.network.receive.ModS2CReceivers;
import net.rotgruengelb.landscape.client.network.receive.LandscapeS2CReceivers;
import net.rotgruengelb.landscape.client.render.block.entity.ZoneBlockBlockEntityRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static net.rotgruengelb.landscape.block.entity.ModBlockEntities.ZONE_BLOCK_BLOCK_ENTITY;
import static net.rotgruengelb.landscape.block.entity.LandscapeBlockEntities.ZONE_BLOCK_BLOCK_ENTITY;

@Environment(EnvType.CLIENT)
public class LandscapeClient implements ClientModInitializer {
Expand All @@ -19,6 +19,6 @@ public class LandscapeClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
BlockEntityRendererFactories.register(ZONE_BLOCK_BLOCK_ENTITY, ZoneBlockBlockEntityRenderer::new);
ModS2CReceivers.registerModS2CReceivers();
LandscapeS2CReceivers.registerModS2CReceivers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import net.minecraft.nbt.StringNbtReader;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.rotgruengelb.landscape.LandscapeClient;
import net.rotgruengelb.landscape.block.ModBlocks;
import net.rotgruengelb.landscape.block.LandscapeBlocks;
import net.rotgruengelb.landscape.block.ZoneBlock;
import net.rotgruengelb.landscape.block.entity.ZoneBlockBlockEntity;
import net.rotgruengelb.landscape.block.enums.ZoneBlockMode;
Expand All @@ -40,7 +41,7 @@ public class ZoneBlockScreen extends Screen {
private final BlockPos pos;
private final ZoneBlockMode originalMode;
private final boolean originalShowZones;
private final String newRuleSet;
private final Identifier newRuleSet;
private final int originalPriority;
private final ClampedNum<Integer> newPriority = new ClampedNum<>(0, 99);
private NbtCompound originalZones;
Expand All @@ -60,7 +61,7 @@ public ZoneBlockScreen(ZoneBlockBlockEntity zoneBlock) {
this.originalShowZones = zoneBlock.shouldShowZones();
this.originalPriority = zoneBlock.getZoneManagerContext().priority();
this.newPriority.adjustSet(this.originalPriority);
this.newRuleSet = zoneBlock.getRuleSet().getIdentifierString();
this.newRuleSet = zoneBlock.getRuleSet();
this.newMode = this.originalMode;
this.newShowZones = this.originalShowZones;
}
Expand Down Expand Up @@ -138,7 +139,7 @@ public boolean charTyped(char chr, int modifiers) {
this.inputRuleSet = new TextFieldWidget(this.textRenderer, this.width / 2 - 152, 120, 300, 20, Text.translatable("text.landscape.zone_block.input_ruleset")) {
};
this.inputRuleSet.setMaxLength(200);
this.inputRuleSet.setText(this.newRuleSet);
this.inputRuleSet.setText(this.newRuleSet.toString());
this.addSelectableChild(this.inputRuleSet);
this.updateWidgets(this.newMode);
this.clientBlockState(this.newMode);
Expand All @@ -148,7 +149,7 @@ private void clientBlockState(ZoneBlockMode mode) {
if (client != null) {
client.execute(() -> {
BlockState blockState = client.world.getBlockState(this.pos);
if (blockState.isOf(ModBlocks.ZONE_BLOCK)) {
if (blockState.isOf(LandscapeBlocks.ZONE_BLOCK)) {
client.world.setBlockState(this.pos, blockState.with(ZoneBlock.MODE, mode), 2);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

import static net.rotgruengelb.landscape.network.constant.PacketIds.S2C_ZONE_MANAGER_SYNC_PACKET_ID;

public class ModS2CReceivers {
public class LandscapeS2CReceivers {
public static void registerModS2CReceivers() {

LandscapeClient.C_LOGGER.debug("Registering ModS2CReceivers for " + Landscape.MOD_ID);
LandscapeClient.C_LOGGER.debug("Registering LandscapeS2CReceivers for " + Landscape.MOD_ID);

// ZONE_MANAGER_SYNC_PACKET RECEIVER
ClientPlayNetworking.registerGlobalReceiver(S2C_ZONE_MANAGER_SYNC_PACKET_ID, (client, handler, buf, responseSender) -> {
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/net/rotgruengelb/landscape/Landscape.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net.rotgruengelb.landscape;

import net.fabricmc.api.ModInitializer;
import net.rotgruengelb.landscape.block.ModBlocks;
import net.rotgruengelb.landscape.block.entity.ModBlockEntities;
import net.rotgruengelb.landscape.command.ModCommandNodes;
import net.rotgruengelb.landscape.item.ModItemGroups;
import net.rotgruengelb.landscape.network.receive.ModC2SReceivers;
import net.rotgruengelb.landscape.util.resource.ModResources;
import net.rotgruengelb.landscape.block.LandscapeBlocks;
import net.rotgruengelb.landscape.block.entity.LandscapeBlockEntities;
import net.rotgruengelb.landscape.command.LandscapeCommandNodes;
import net.rotgruengelb.landscape.item.LandscapeItemGroups;
import net.rotgruengelb.landscape.network.receive.LandscapeC2SReceivers;
import net.rotgruengelb.landscape.registry.LandscapeRegistries;
import net.rotgruengelb.nixienaut.annotation.CallerAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,12 +21,12 @@ public class Landscape implements ModInitializer {
@Override
public void onInitialize() {
if (DEV_ENV) { LOGGER.warn("Landscape is running in development environment!"); }
ModCommandNodes.registerModCommandNodes();
ModBlocks.registerModBlocks();
ModBlockEntities.registerModBlockEntities();
ModC2SReceivers.registerModC2SReceivers();
ModResources.registerModResourceReloadListeners();
ModItemGroups.registerItemGroups();
LandscapeCommandNodes.registerModCommandNodes();
LandscapeBlocks.registerModBlocks();
LandscapeBlockEntities.registerModBlockEntities();
LandscapeC2SReceivers.registerModC2SReceivers();
LandscapeItemGroups.registerItemGroups();
LandscapeRegistries.registerDynamicRegistries();
}

@CallerAware
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import net.rotgruengelb.landscape.Landscape;
import net.rotgruengelb.landscape.item.ZoneBlockItem;

public class ModBlocks {
public class LandscapeBlocks {

public static final Block ZONE_BLOCK = registerBlockNoItem("zone_block", new ZoneBlock(FabricBlockSettings.copyOf(Blocks.STRUCTURE_BLOCK)));
public static final Item ZONE_BLOCK_ITEM = registerBlockItem("zone_block", new ZoneBlockItem(ZONE_BLOCK, new FabricItemSettings()));
Expand All @@ -32,6 +32,6 @@ private static BlockItem registerBlockItem(String name, BlockItem blockItem) {
}

public static void registerModBlocks() {
Landscape.LOGGER.debug("Registering ModBlocks for " + Landscape.MOD_ID);
Landscape.LOGGER.debug("Registering LandscapeBlocks for " + Landscape.MOD_ID);
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/rotgruengelb/landscape/block/ZoneBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import net.rotgruengelb.landscape.Landscape;
import net.rotgruengelb.landscape.block.entity.ZoneBlockBlockEntity;
import net.rotgruengelb.landscape.block.enums.ZoneBlockMode;
import net.rotgruengelb.landscape.state.ModProperties;
import net.rotgruengelb.landscape.state.LandscapeProperties;

public class ZoneBlock extends BlockWithEntity implements OperatorBlock, BlockEntityProvider {
public static final EnumProperty<ZoneBlockMode> MODE = ModProperties.ZONE_BLOCK_MODE;
public static final EnumProperty<ZoneBlockMode> MODE = LandscapeProperties.ZONE_BLOCK_MODE;
public static final DirectionProperty FACING = HorizontalFacingBlock.FACING;
public static final MapCodec<ZoneBlock> CODEC = ZoneBlock.createCodec(ZoneBlock::new);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.rotgruengelb.landscape.Landscape;
import net.rotgruengelb.landscape.block.ModBlocks;
import net.rotgruengelb.landscape.block.LandscapeBlocks;

public class ModBlockEntities {
public class LandscapeBlockEntities {
public static void registerModBlockEntities() {
Landscape.LOGGER.debug("Registering ModBlockEntities for " + Landscape.MOD_ID);
Landscape.LOGGER.debug("Registering LandscapeBlockEntities for " + Landscape.MOD_ID);
}

public static final BlockEntityType<ZoneBlockBlockEntity> ZONE_BLOCK_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, Registries.BLOCK.getId(ModBlocks.ZONE_BLOCK), FabricBlockEntityTypeBuilder.create(ZoneBlockBlockEntity::new, ModBlocks.ZONE_BLOCK)
public static final BlockEntityType<ZoneBlockBlockEntity> ZONE_BLOCK_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, Registries.BLOCK.getId(LandscapeBlocks.ZONE_BLOCK), FabricBlockEntityTypeBuilder.create(ZoneBlockBlockEntity::new, LandscapeBlocks.ZONE_BLOCK)
.build());
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,41 @@
import net.minecraft.world.World;
import net.rotgruengelb.landscape.Landscape;
import net.rotgruengelb.landscape.accessor.LandscapeClientPlayerEntity;
import net.rotgruengelb.landscape.block.ModBlocks;
import net.rotgruengelb.landscape.block.LandscapeBlocks;
import net.rotgruengelb.landscape.block.ZoneBlock;
import net.rotgruengelb.landscape.block.enums.ZoneBlockMode;
import net.rotgruengelb.landscape.feature.zones.ZoneManager;
import net.rotgruengelb.landscape.feature.zones.manager.AvailableZoneManagers;
import net.rotgruengelb.landscape.feature.zones.manager.context.ZoneManagerContext;
import net.rotgruengelb.landscape.feature.zones.rule.AvailableRuleSets;
import net.rotgruengelb.landscape.feature.zones.rule.RuleSet;
import net.rotgruengelb.landscape.util.math.BlockZone;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class ZoneBlockBlockEntity extends BlockEntity implements BlockEntityProvider, ZoneManager {

private static final HashMap<ZoneBlockMode, RuleSet> MODE_RULESETS = new HashMap<>() {{
private static final HashMap<ZoneBlockMode, Identifier> MODE_RULESETS = new HashMap<>() {{
put(ZoneBlockMode.CUSTOM_RULESET, null);
put(ZoneBlockMode.TRIGGER, RuleSet.of(AvailableRuleSets.EMPTY_RULESET));
put(ZoneBlockMode.DENY_WORLD_MODIFY, RuleSet.of("landscape:rulesets/zone_block/deny_world_modify"));
put(ZoneBlockMode.ALLOW_WORLD_MODIFY, RuleSet.of("landscape:rulesets/zone_block/allow_world_modify"));
put(ZoneBlockMode.DENY_BREAK, RuleSet.of("landscape:rulesets/zone_block/deny_break"));
put(ZoneBlockMode.ALLOW_BREAK, RuleSet.of("landscape:rulesets/zone_block/allow_break"));
put(ZoneBlockMode.DENY_PLACE, RuleSet.of("landscape:rulesets/zone_block/deny_place"));
put(ZoneBlockMode.ALLOW_PLACE, RuleSet.of("landscape:rulesets/zone_block/allow_place"));
put(ZoneBlockMode.TRIGGER, RuleSet.EMPTY_RULESET_ID);
put(ZoneBlockMode.DENY_WORLD_MODIFY, new Identifier("landscape:zone_block/deny_world_modify"));
put(ZoneBlockMode.ALLOW_WORLD_MODIFY, new Identifier("landscape:zone_block/allow_world_modify"));
put(ZoneBlockMode.DENY_BREAK, new Identifier("landscape:zone_block/deny_break"));
put(ZoneBlockMode.ALLOW_BREAK, new Identifier("landscape:zone_block/allow_break"));
put(ZoneBlockMode.DENY_PLACE, new Identifier("landscape:zone_block/deny_place"));
put(ZoneBlockMode.ALLOW_PLACE, new Identifier("landscape:zone_block/allow_place"));
}};
private ZoneBlockMode mode;
private boolean showZones = false;
private boolean powered;
private RuleSet ruleSet = RuleSet.of(AvailableRuleSets.EMPTY_RULESET);
private Identifier ruleSet = RuleSet.EMPTY_RULESET_ID;
private int priority = 0;
private List<BlockZone> zones = new ArrayList<>();

public ZoneBlockBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ZONE_BLOCK_BLOCK_ENTITY, pos, state);
super(LandscapeBlockEntities.ZONE_BLOCK_BLOCK_ENTITY, pos, state);
this.mode = state.get(ZoneBlock.MODE);
this.zones.add(new BlockZone(new BlockPos(1, 0, 1), new BlockPos(11, 10, 11)));
}
Expand All @@ -60,7 +57,7 @@ protected void writeNbt(NbtCompound nbt) {
super.writeNbt(nbt);
nbt.putBoolean("showZones", this.showZones);
nbt.putInt("priority", this.priority);
nbt.putString("ruleSet", this.ruleSet.getIdentifierString());
nbt.putString("ruleSet", this.ruleSet.toString());
nbt.put("zones", this.zonesToNbt());
nbt.putString("mode", this.mode.toString());
}
Expand All @@ -86,8 +83,7 @@ public void readNbt(NbtCompound nbt) {
} catch (IllegalArgumentException e) {
this.mode = ZoneBlockMode.TRIGGER;
}
this.ruleSet = RuleSet.of(Objects.requireNonNullElse(RuleSet.of(Identifier.tryParse(nbt.getString("ruleSet")))
.getIdentifier(), AvailableRuleSets.EMPTY_RULESET));
this.ruleSet = Identifier.tryParse(nbt.getString("ruleSet"));
this.updateBlockMode();
this.updateContext();
}
Expand All @@ -105,7 +101,7 @@ private void updateBlockMode() {
Landscape.LOGGER.debug("Updating block mode");
BlockPos blockPos = this.getPos();
BlockState blockState = this.world.getBlockState(blockPos);
if (blockState.isOf(ModBlocks.ZONE_BLOCK)) {
if (blockState.isOf(LandscapeBlocks.ZONE_BLOCK)) {
this.world.setBlockState(blockPos, blockState.with(ZoneBlock.MODE, this.mode), 2);
}
}
Expand Down Expand Up @@ -215,24 +211,24 @@ public List<BlockZone> getZones(boolean wantRaw) {
return rotatedZones;
}

public @NotNull RuleSet getRuleSet() {
public Identifier getRuleSet() {
if (ruleSet == null) {
return RuleSet.of(AvailableRuleSets.EMPTY_RULESET);
return RuleSet.EMPTY_RULESET_ID;
}
return this.ruleSet;
}

public void setRuleSet(RuleSet ruleSet) {
public void setRuleSet(Identifier ruleSet) {
if (ruleSet == null) {
this.ruleSet = RuleSet.of(AvailableRuleSets.EMPTY_RULESET);
this.ruleSet = RuleSet.EMPTY_RULESET_ID;
return;
}
this.ruleSet = ruleSet;
}

public RuleSet getRuleSet(boolean useMode) {
RuleSet returnRuleSet = this.ruleSet != null ? this.ruleSet : RuleSet.of(AvailableRuleSets.EMPTY_RULESET);
RuleSet gotRuleSet = MODE_RULESETS.get(this.mode);
public Identifier getRuleSet(boolean useMode) {
Identifier returnRuleSet = this.ruleSet != null ? this.ruleSet : RuleSet.EMPTY_RULESET_ID;
Identifier gotRuleSet = MODE_RULESETS.get(this.mode);
if (useMode && gotRuleSet != null) {
returnRuleSet = gotRuleSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import net.rotgruengelb.landscape.Landscape;
import net.rotgruengelb.landscape.command.debuglet.Debuglets;

public class ModCommandNodes {
public class LandscapeCommandNodes {

public static void registerModCommandNodes() {

Landscape.LOGGER.debug("Registering ModCommandNodes for " + Landscape.MOD_ID);
Landscape.LOGGER.debug("Registering LandscapeCommandNodes for " + Landscape.MOD_ID);

CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import net.rotgruengelb.landscape.feature.zones.rule.RuleSet;

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

public class RuleSetsDebuglet {
public static int showAvailableRuleSets(CommandContext<ServerCommandSource> ctx) {
Map<Identifier, RuleSet> rulesets = AvailableRuleSets.getRuleSets();
for (Identifier ruleSet : rulesets.keySet()) {
Set<Identifier> rulesets = AvailableRuleSets.getRuleSets();
for (Identifier ruleSet : rulesets) {
ctx.getSource()
.sendMessage(Text.literal("ruleset: " + ruleSet + " -> " + AvailableRuleSets.getRuleSet(ruleSet)
.getName()));
.name()));
}
return 1;
}
Expand All @@ -29,7 +30,7 @@ public static int showRuleSetRules(CommandContext<ServerCommandSource> ctx) {
.sendError(Text.literal("No RuleSet with name " + ruleSetName + " found"));
return 0;
}
Map<String, Boolean> rules = ruleSet.getRules();
Map<String, Boolean> rules = ruleSet.rules();
for (String rule : rules.keySet()) {
ctx.getSource().sendMessage(Text.literal("rule: " + rule + " -> " + rules.get(rule)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.rotgruengelb.landscape.Landscape;
import net.rotgruengelb.landscape.feature.zones.manager.AvailableZoneManagers;
import net.rotgruengelb.landscape.feature.zones.manager.context.ZoneManagerContext;
import net.rotgruengelb.landscape.feature.zones.rule.RuleSet;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand All @@ -13,7 +14,7 @@

import static net.rotgruengelb.landscape.util.Util.mapWins;

public class API {
public class LandscapeAPI {

public static Optional<Boolean> allowsAction(BlockPos pos, String rule, World world, boolean includeNull) {
if (pos == null || rule == null || world == null) {
Expand All @@ -29,9 +30,9 @@ public static Optional<Boolean> allowsAction(BlockPos pos, String rule, World wo
zoneManagerContexts.stream()
.filter(zoneManagerContext -> zoneManagerContext.isBlockPosInZone(pos, false))
.forEach(zoneManagerContext -> {
if (zoneManagerContext.ruleSet().containsRule(rule)) {
values.put(zoneManagerContext.priority(), zoneManagerContext.ruleSet()
.getRuleValue(rule));
RuleSet ruleSet = RuleSet.of(zoneManagerContext.ruleSet());
if (ruleSet.containsRule(rule)) {
values.put(zoneManagerContext.priority(), ruleSet.getRuleValue(rule));
}
});

Expand Down

0 comments on commit 5e8ce88

Please sign in to comment.