diff --git a/src/main/java/dev/enjarai/minitardis/MiniTardis.java b/src/main/java/dev/enjarai/minitardis/MiniTardis.java index ec4f37b..bd97963 100644 --- a/src/main/java/dev/enjarai/minitardis/MiniTardis.java +++ b/src/main/java/dev/enjarai/minitardis/MiniTardis.java @@ -5,6 +5,7 @@ import dev.enjarai.minitardis.component.ModComponents; import dev.enjarai.minitardis.component.Tardis; import dev.enjarai.minitardis.data.TardisInteriorManager; +import dev.enjarai.minitardis.item.ModItems; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; @@ -48,6 +49,7 @@ public void onInitialize() { ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(interiorManager); ModBlocks.load(); + ModItems.load(); } @Nullable diff --git a/src/main/java/dev/enjarai/minitardis/block/InteriorDoorBlock.java b/src/main/java/dev/enjarai/minitardis/block/InteriorDoorBlock.java index 149390a..4906158 100644 --- a/src/main/java/dev/enjarai/minitardis/block/InteriorDoorBlock.java +++ b/src/main/java/dev/enjarai/minitardis/block/InteriorDoorBlock.java @@ -12,7 +12,7 @@ import net.minecraft.world.World; @SuppressWarnings("deprecation") -public class InteriorDoorBlock extends FacingBlock implements PolymerBlock, TardisAwareBlock { +public class InteriorDoorBlock extends FacingBlock implements PolymerBlock, TardisAware { protected InteriorDoorBlock(Settings settings) { super(settings); setDefaultState(getStateManager().getDefaultState().with(FACING, Direction.NORTH)); diff --git a/src/main/java/dev/enjarai/minitardis/block/ModBlocks.java b/src/main/java/dev/enjarai/minitardis/block/ModBlocks.java index 29b72ab..04a11ca 100644 --- a/src/main/java/dev/enjarai/minitardis/block/ModBlocks.java +++ b/src/main/java/dev/enjarai/minitardis/block/ModBlocks.java @@ -1,6 +1,11 @@ package dev.enjarai.minitardis.block; import dev.enjarai.minitardis.MiniTardis; +import dev.enjarai.minitardis.block.console.ConsoleLeverBlock; +import dev.enjarai.minitardis.block.console.ConsoleScreenBlock; +import dev.enjarai.minitardis.block.console.ConsoleScreenBlockEntity; +import dev.enjarai.minitardis.component.TardisControl; +import eu.pb4.polymer.core.api.block.PolymerBlock; import eu.pb4.polymer.core.api.block.PolymerBlockUtils; import eu.pb4.polymer.core.api.block.SimplePolymerBlock; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; @@ -14,6 +19,8 @@ import net.minecraft.registry.Registry; import net.minecraft.world.poi.PointOfInterestType; +import java.util.List; + public class ModBlocks { public static final TardisExteriorBlock TARDIS_EXTERIOR = register("tardis_exterior", new TardisExteriorBlock(FabricBlockSettings.create())); @@ -21,15 +28,23 @@ public class ModBlocks { register("tardis_plating", new SimplePolymerBlock(FabricBlockSettings.create(), Blocks.DEAD_BRAIN_CORAL_BLOCK)); public static final InteriorDoorBlock INTERIOR_DOOR = register("interior_door", new InteriorDoorBlock(FabricBlockSettings.create())); + public static final ConsoleLeverBlock HANDBRAKE = + register("handbrake", new ConsoleLeverBlock(FabricBlockSettings.create(), TardisControl::handbrake)); + public static final ConsoleScreenBlock CONSOLE_SCREEN = + register("console_screen", new ConsoleScreenBlock(FabricBlockSettings.create())); public static final BlockEntityType TARDIS_EXTERIOR_ENTITY = registerEntity("tardis_exterior", TardisExteriorBlockEntity::new, TARDIS_EXTERIOR); + public static final BlockEntityType CONSOLE_SCREEN_ENTITY = + registerEntity("console_screen", ConsoleScreenBlockEntity::new, CONSOLE_SCREEN); public static final PointOfInterestType TARDIS_EXTERIOR_POI = PointOfInterestHelper.register(MiniTardis.id("tardis_exterior"), 0, 1, TARDIS_EXTERIOR); public static final PointOfInterestType INTERIOR_DOOR_POI = PointOfInterestHelper.register(MiniTardis.id("interior_door"), 0, 1, INTERIOR_DOOR); + public static final List ITEM_BLOCKS = List.of(TARDIS_PLATING, INTERIOR_DOOR, HANDBRAKE); + public static void load() { } diff --git a/src/main/java/dev/enjarai/minitardis/block/TardisAwareBlock.java b/src/main/java/dev/enjarai/minitardis/block/TardisAware.java similarity index 90% rename from src/main/java/dev/enjarai/minitardis/block/TardisAwareBlock.java rename to src/main/java/dev/enjarai/minitardis/block/TardisAware.java index add788d..8b86352 100644 --- a/src/main/java/dev/enjarai/minitardis/block/TardisAwareBlock.java +++ b/src/main/java/dev/enjarai/minitardis/block/TardisAware.java @@ -6,7 +6,7 @@ import java.util.Optional; -public interface TardisAwareBlock { +public interface TardisAware { default Optional getTardis(World world) { return world.getComponent(ModComponents.TARDIS_REFERENCE).getTardis(); } diff --git a/src/main/java/dev/enjarai/minitardis/block/console/ConsoleLeverBlock.java b/src/main/java/dev/enjarai/minitardis/block/console/ConsoleLeverBlock.java new file mode 100644 index 0000000..ece55ba --- /dev/null +++ b/src/main/java/dev/enjarai/minitardis/block/console/ConsoleLeverBlock.java @@ -0,0 +1,56 @@ +package dev.enjarai.minitardis.block.console; + +import dev.enjarai.minitardis.block.TardisAware; +import dev.enjarai.minitardis.component.TardisControl; +import eu.pb4.polymer.core.api.block.PolymerBlock; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.LeverBlock; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.world.BlockView; +import net.minecraft.world.World; + +import java.util.function.BiConsumer; + +public class ConsoleLeverBlock extends LeverBlock implements PolymerBlock, TardisAware { + private final BiConsumer controlInput; + + public ConsoleLeverBlock(Settings settings, BiConsumer controlInput) { + super(settings); + this.controlInput = controlInput; + } + + @Override + public BlockState togglePower(BlockState state, World world, BlockPos pos) { + var state2 = super.togglePower(state, world, pos); + getTardis(world).ifPresent(tardis -> controlInput.accept(tardis.getControls(), state2.get(POWERED))); + return state2; + } + + @Override + public int getWeakRedstonePower(BlockState state, BlockView world, BlockPos pos, Direction direction) { + return 0; + } + + @Override + public int getStrongRedstonePower(BlockState state, BlockView world, BlockPos pos, Direction direction) { + return 0; + } + + @Override + public boolean emitsRedstonePower(BlockState state) { + return false; + } + + @Override + public Block getPolymerBlock(BlockState state) { + return Blocks.LEVER; + } + + @Override + public BlockState getPolymerBlockState(BlockState state) { + return Blocks.LEVER.getStateWithProperties(state); + } +} diff --git a/src/main/java/dev/enjarai/minitardis/block/console/ConsoleScreenBlock.java b/src/main/java/dev/enjarai/minitardis/block/console/ConsoleScreenBlock.java new file mode 100644 index 0000000..e76c498 --- /dev/null +++ b/src/main/java/dev/enjarai/minitardis/block/console/ConsoleScreenBlock.java @@ -0,0 +1,99 @@ +package dev.enjarai.minitardis.block.console; + +import dev.enjarai.minitardis.block.ModBlocks; +import dev.enjarai.minitardis.block.TardisAware; +import eu.pb4.polymer.core.api.block.PolymerBlock; +import eu.pb4.polymer.virtualentity.api.BlockWithElementHolder; +import eu.pb4.polymer.virtualentity.api.ElementHolder; +import eu.pb4.polymer.virtualentity.api.elements.TextDisplayElement; +import net.minecraft.block.*; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityTicker; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.state.StateManager; +import net.minecraft.state.property.DirectionProperty; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; + +public class ConsoleScreenBlock extends BlockWithEntity implements PolymerBlock, TardisAware, BlockWithElementHolder { + public static final DirectionProperty FACING = HorizontalFacingBlock.FACING; + + public ConsoleScreenBlock(Settings settings) { + super(settings); + setDefaultState(getStateManager().getDefaultState().with(FACING, Direction.NORTH)); + } + + @Nullable + @Override + public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { + return new ConsoleScreenBlockEntity(pos, state); + } + + @Nullable + @Override + public BlockEntityTicker getTicker(World world, BlockState state, BlockEntityType type) { + return validateTicker(type, ModBlocks.CONSOLE_SCREEN_ENTITY, (world1, pos, state1, blockEntity) -> blockEntity.tick(world1, pos, state1)); + } + + @Override + protected void appendProperties(StateManager.Builder builder) { + builder.add(FACING); + } + + @Override + public Block getPolymerBlock(BlockState state) { + return Blocks.OAK_WALL_SIGN; + } + + @Override + public BlockState getPolymerBlockState(BlockState state) { + return getPolymerBlock(state).getStateWithProperties(state); + } + + @Override + public boolean tickElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) { + return true; + } + + @Override + public @Nullable ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) { + var tardisOptional = getTardis(world); + if (tardisOptional.isPresent()) { + var tardis = tardisOptional.get(); + var textDisplay = new TextDisplayElement(); + + return new ElementHolder() { + { + addElement(textDisplay); + } + + @Override + protected void onTick() { + + } + + private void update() { + textDisplay.setText(tardis.getState().getName()); + } + }; + } + return null; + } + + // @Override +// public void onPolymerBlockSend(BlockState blockState, BlockPos.Mutable pos, ServerPlayerEntity player) { +// var main = new NbtCompound(); +// main.putString("id", "minecraft:sign"); +// main.putInt("x", pos.getX()); +// main.putInt("y", pos.getY()); +// main.putInt("z", pos.getZ()); +// main.putBoolean("is_waxed", true); +// var frontText = new NbtCompound(); +// var messages = new NbtList(); +// messages.add() +// player.networkHandler.sendPacket(PolymerBlockUtils.createBlockEntityPacket(pos.toImmutable(), BlockEntityType.SIGN, main)); +// } +} diff --git a/src/main/java/dev/enjarai/minitardis/block/console/ConsoleScreenBlockEntity.java b/src/main/java/dev/enjarai/minitardis/block/console/ConsoleScreenBlockEntity.java new file mode 100644 index 0000000..2efafa8 --- /dev/null +++ b/src/main/java/dev/enjarai/minitardis/block/console/ConsoleScreenBlockEntity.java @@ -0,0 +1,18 @@ +package dev.enjarai.minitardis.block.console; + +import dev.enjarai.minitardis.block.ModBlocks; +import dev.enjarai.minitardis.block.TardisAware; +import net.minecraft.block.BlockState; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public class ConsoleScreenBlockEntity extends BlockEntity implements TardisAware { + public ConsoleScreenBlockEntity(BlockPos pos, BlockState state) { + super(ModBlocks.CONSOLE_SCREEN_ENTITY, pos, state); + } + + public void tick(World world, BlockPos pos, BlockState state) { + + } +} diff --git a/src/main/java/dev/enjarai/minitardis/component/Tardis.java b/src/main/java/dev/enjarai/minitardis/component/Tardis.java index 7875b11..3df4c2c 100644 --- a/src/main/java/dev/enjarai/minitardis/component/Tardis.java +++ b/src/main/java/dev/enjarai/minitardis/component/Tardis.java @@ -257,6 +257,10 @@ public boolean suggestStateTransition(FlightState newState) { return accepted; } + public FlightState getState() { + return state; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/dev/enjarai/minitardis/component/flight/FlightState.java b/src/main/java/dev/enjarai/minitardis/component/flight/FlightState.java index b823569..9691c04 100644 --- a/src/main/java/dev/enjarai/minitardis/component/flight/FlightState.java +++ b/src/main/java/dev/enjarai/minitardis/component/flight/FlightState.java @@ -2,6 +2,7 @@ import com.mojang.serialization.Codec; import dev.enjarai.minitardis.component.Tardis; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import java.util.Map; @@ -52,4 +53,8 @@ default boolean isSolid(Tardis tardis) { * A unique id for serialization */ Identifier id(); + + default Text getName() { + return Text.translatable("mini_tardis.state." + id().getNamespace() + "." + id().getPath()); + } } diff --git a/src/main/java/dev/enjarai/minitardis/item/ModItems.java b/src/main/java/dev/enjarai/minitardis/item/ModItems.java new file mode 100644 index 0000000..3c607a3 --- /dev/null +++ b/src/main/java/dev/enjarai/minitardis/item/ModItems.java @@ -0,0 +1,20 @@ +package dev.enjarai.minitardis.item; + +import dev.enjarai.minitardis.block.ModBlocks; +import eu.pb4.polymer.core.api.block.PolymerBlock; +import eu.pb4.polymer.core.api.item.PolymerBlockItem; +import net.fabricmc.fabric.api.item.v1.FabricItemSettings; +import net.minecraft.registry.Registries; +import net.minecraft.registry.Registry; + +public class ModItems { + public static void load() { + ModBlocks.ITEM_BLOCKS.forEach(block -> { + if (block instanceof PolymerBlock polymerBlock) { + var id = Registries.BLOCK.getId(block); + var polymerItem = polymerBlock.getPolymerBlock(block.getDefaultState()).asItem(); + Registry.register(Registries.ITEM, id, new PolymerBlockItem(block, new FabricItemSettings(), polymerItem)); + } + }); + } +}