Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod_name=Create: Maintenance Control
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPL-3.0 License
# The mod version. See https://semver.org/
mod_version=1.1.3-mc1.21.1
mod_version=1.2.0-mc1.21.1
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.dantemc.create_maintenance_control;

import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.common.ModConfigSpec;

public class Config {
Expand All @@ -12,8 +10,4 @@ public class Config {
.define("debugLogs", false);

static final ModConfigSpec SPEC = BUILDER.build();

private static boolean validateItemName(final Object obj) {
return obj instanceof String itemName && BuiltInRegistries.ITEM.containsKey(ResourceLocation.parse(itemName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.dantemc.create_maintenance_control.foundation.IgnorePlacingRulesItem;
import net.dantemc.create_maintenance_control.content.maintenance_box.MaintenanceBoxBlock;
import net.dantemc.create_maintenance_control.content.maintenance_box.MaintenanceBoxBlockEntity;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.neoforged.bus.api.IEventBus;
Expand Down Expand Up @@ -54,9 +55,15 @@ public CreateMaintenance(IEventBus modEventBus, ModContainer modContainer) {

NeoForge.EVENT_BUS.register(this);

ModPackets.register();

modContainer.registerConfig(ModConfig.Type.COMMON, Config.SPEC);
}

public static ResourceLocation asResource(String path) {
return ResourceLocation.fromNamespaceAndPath(MODID, path);
}

//helper method for loggers
public static void debug(String message, Object... args) {
if (Config.WRITE_DEBUG_LOGS.get()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.dantemc.create_maintenance_control;

import java.util.Locale;

import net.createmod.catnip.net.base.BasePacketPayload;
import net.createmod.catnip.net.base.CatnipPacketRegistry;
import net.dantemc.create_maintenance_control.content.maintenance_box.gui.MaintenanceBoxConfigurationPacket;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;

public enum ModPackets implements BasePacketPayload.PacketTypeProvider {
CONFIGURE_MAINTENANCE_BOX(
MaintenanceBoxConfigurationPacket.class,
MaintenanceBoxConfigurationPacket.STREAM_CODEC
);

private final CatnipPacketRegistry.PacketType<?> type;

<T extends BasePacketPayload> ModPackets(Class<T> clazz, StreamCodec<? super RegistryFriendlyByteBuf, T> codec) {
String name = this.name().toLowerCase(Locale.ROOT);
this.type = new CatnipPacketRegistry.PacketType<>(
new CustomPacketPayload.Type<>(CreateMaintenance.asResource(name)),
clazz, codec
);
}

@Override
@SuppressWarnings("unchecked")
public <T extends CustomPacketPayload> CustomPacketPayload.Type<T> getType() {
return (CustomPacketPayload.Type<T>) this.type.type();
}

public static void register() {
CatnipPacketRegistry packetRegistry =
new CatnipPacketRegistry(CreateMaintenance.MODID, "1.0.0");
for (ModPackets packet : ModPackets.values()) {
packetRegistry.registerPacket(packet.type);
}
packetRegistry.registerAllPackets();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package net.dantemc.create_maintenance_control.content.maintenance_box;

import com.simibubi.create.content.trains.station.GlobalStation;
import com.simibubi.create.foundation.block.IBE;
import com.simibubi.create.foundation.block.WrenchableDirectionalBlock;
import net.createmod.catnip.gui.ScreenOpener;
import net.dantemc.create_maintenance_control.CreateMaintenance;
import net.dantemc.create_maintenance_control.content.maintenance_box.gui.MaintenanceBoxScreen;
import net.dantemc.create_maintenance_control.foundation.CreateMaintenanceShapes;
import net.dantemc.create_maintenance_control.railway.OfflineStationManager;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
Expand All @@ -19,8 +23,11 @@
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;

public class MaintenanceBoxBlock extends WrenchableDirectionalBlock implements IBE<MaintenanceBoxBlockEntity> {

Expand All @@ -31,6 +38,14 @@ public MaintenanceBoxBlock(Properties properties) {
registerDefaultState(defaultBlockState().setValue(POWERED, false));
}

private void refreshAttachedStationBox(Level level, BlockPos pos, boolean powered, boolean skipDownstream) {
GlobalStation station = StationUtils.findNearbyStation(level, pos);
if (station == null)
return;

OfflineStationManager.refreshBox(level, pos, powered, skipDownstream);
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(POWERED);
Expand All @@ -50,10 +65,7 @@ public void neighborChanged(BlockState state, Level level, BlockPos pos, Block b

level.setBlock(pos, state.setValue(POWERED, powered), Block.UPDATE_ALL);

GlobalStation station = StationUtils.findNearbyStation(level, pos);
if (station == null)
return;
OfflineStationManager.registerBox(level, pos, station.name, !powered);
refreshAttachedStationBox(level, pos, powered, false);
}

@Override
Expand All @@ -73,10 +85,7 @@ public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldS
level.setBlock(pos, state, Block.UPDATE_ALL);
}

GlobalStation station = StationUtils.findNearbyStation(level, pos);
if (station == null)
return;
OfflineStationManager.registerBox(level, pos, station.name, !powered);
refreshAttachedStationBox(level, pos, powered, false);
}

@Override
Expand All @@ -94,6 +103,13 @@ public void onRemove(BlockState state, Level level, BlockPos pos, BlockState new
OfflineStationManager.unregisterBox(level, pos);
}

@OnlyIn(value = Dist.CLIENT)
protected void displayScreen(MaintenanceBoxBlockEntity be, Player player) {
if (!(player instanceof LocalPlayer))
return;
ScreenOpener.open(new MaintenanceBoxScreen(be));
}

@Override
public Class<MaintenanceBoxBlockEntity> getBlockEntityClass() {
return MaintenanceBoxBlockEntity.class;
Expand All @@ -115,6 +131,15 @@ public VoxelShape getShape(BlockState State, BlockGetter Level, BlockPos Pos, Co
return CreateMaintenanceShapes.MAINTENANCE_BOX.get(State.getValue(FACING));
}

@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {
if (level.isClientSide) {
withBlockEntityDo(level, pos, be -> displayScreen(be, player));
}

return InteractionResult.SUCCESS;
}

@Override
protected void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,78 @@
package net.dantemc.create_maintenance_control.content.maintenance_box;

import com.simibubi.create.content.trains.station.GlobalStation;
import com.simibubi.create.foundation.blockEntity.SyncedBlockEntity;
import net.dantemc.create_maintenance_control.CreateMaintenance;
import net.dantemc.create_maintenance_control.railway.MaintenanceEntry;
import net.dantemc.create_maintenance_control.railway.OfflineStationManager;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

public class MaintenanceBoxBlockEntity extends BlockEntity {
public class MaintenanceBoxBlockEntity extends SyncedBlockEntity {

public MaintenanceBoxBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}

private String stationFilter = "";
private MaintenanceRedstoneMode redstoneMode = MaintenanceRedstoneMode.UNPOWERED_ACTIVE;
private boolean skipDownstream = false;

public String getStationFilter() {
return stationFilter;
}

public MaintenanceRedstoneMode getRedstoneMode() {
return redstoneMode;
}

public boolean shouldSkipDownstream() {
return skipDownstream;
}

public void registerStation() {
Level level = getLevel();

if (level == null || level.isClientSide)
return;

GlobalStation station = StationUtils.findNearbyStation(level, getBlockPos());

if (station == null)
return;

boolean powered = getBlockState().getValue(MaintenanceBoxBlock.POWERED);

OfflineStationManager.registerBox(level, getBlockPos(), station.name, !powered);
MaintenanceEntry entry = OfflineStationManager.ensureBoxEntry(level, getBlockPos(), station.name);

this.stationFilter = entry.stationFilter();
this.redstoneMode = entry.redstoneMode();

OfflineStationManager.refreshBox(level, getBlockPos(), powered, skipDownstream);

setChanged();
notifyUpdate();
}

public void applySettings(String stationFilter, MaintenanceRedstoneMode redstoneMode, boolean skipDownstream) {
Level level = getLevel();
if (level == null || level.isClientSide)
return;

this.stationFilter = stationFilter;
this.redstoneMode = redstoneMode;
this.skipDownstream = skipDownstream;

OfflineStationManager.updateBoxSettings(level, getBlockPos(), stationFilter, redstoneMode, skipDownstream);

boolean powered = getBlockState().getValue(MaintenanceBoxBlock.POWERED);
OfflineStationManager.refreshBox(level, getBlockPos(), powered, skipDownstream);

setChanged();
notifyUpdate();
}

@Override
Expand All @@ -42,10 +86,32 @@ public void onLoad() {

CreateMaintenance.debug("Scheduling station registration");

level.scheduleTick(
getBlockPos(),
getBlockState().getBlock(),
100
);
level.scheduleTick(getBlockPos(), getBlockState().getBlock(), 100);
}

@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);

tag.putString("StationFilter", stationFilter);
tag.putString("RedstoneMode", redstoneMode.name());
tag.putBoolean("SkipDownstream", skipDownstream);
}

@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);

stationFilter = tag.getString("StationFilter");

try {
redstoneMode = MaintenanceRedstoneMode.valueOf(tag.getString("RedstoneMode"));
} catch (IllegalArgumentException e) {
redstoneMode = MaintenanceRedstoneMode.UNPOWERED_ACTIVE;
}

if (tag.contains("SkipDownstream")) {
skipDownstream = tag.getBoolean("SkipDownstream");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.dantemc.create_maintenance_control.content.maintenance_box;

public enum MaintenanceRedstoneMode {
UNPOWERED_ACTIVE {
@Override
public boolean isMaintenanceActive(boolean powered) {
return !powered;
}
},
POWERED_ACTIVE {
@Override
public boolean isMaintenanceActive(boolean powered) {
return powered;
}
};

public abstract boolean isMaintenanceActive(boolean powered);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ public static GlobalStation findNearbyStation(Level level, BlockPos pos) {
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.dantemc.create_maintenance_control.content.maintenance_box.gui;

import com.simibubi.create.foundation.networking.BlockEntityConfigurationPacket;
import io.netty.buffer.ByteBuf;
import net.dantemc.create_maintenance_control.ModPackets;
import net.dantemc.create_maintenance_control.content.maintenance_box.MaintenanceBoxBlockEntity;
import net.dantemc.create_maintenance_control.content.maintenance_box.MaintenanceRedstoneMode;
import net.minecraft.core.BlockPos;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.server.level.ServerPlayer;

public class MaintenanceBoxConfigurationPacket extends BlockEntityConfigurationPacket<MaintenanceBoxBlockEntity> {

public static final StreamCodec<ByteBuf, MaintenanceBoxConfigurationPacket> STREAM_CODEC = StreamCodec.composite(
BlockPos.STREAM_CODEC, packet -> packet.pos,
ByteBufCodecs.STRING_UTF8, packet -> packet.stationFilter,
ByteBufCodecs.VAR_INT, packet -> packet.redstoneMode.ordinal(),
ByteBufCodecs.BOOL, packet -> packet.skipDownstream,
(pos, stationFilter, redstoneModeOrdinal, skipDownstream) ->
new MaintenanceBoxConfigurationPacket(pos, stationFilter, MaintenanceRedstoneMode.values()[redstoneModeOrdinal], skipDownstream)
);

private final String stationFilter;
private final MaintenanceRedstoneMode redstoneMode;
private final boolean skipDownstream;

public MaintenanceBoxConfigurationPacket(BlockPos pos, String stationFilter, MaintenanceRedstoneMode redstoneMode, boolean skipDownstream) {
super(pos);
this.stationFilter = stationFilter;
this.redstoneMode = redstoneMode;
this.skipDownstream = skipDownstream;
}

@Override
protected void applySettings(ServerPlayer player, MaintenanceBoxBlockEntity be) {
be.applySettings(stationFilter, redstoneMode, skipDownstream);
}

@Override
public PacketTypeProvider getTypeProvider() {
return ModPackets.CONFIGURE_MAINTENANCE_BOX;
}
}
Loading
Loading