Skip to content

Commit

Permalink
Hm, we keeping this entity?
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Nov 15, 2023
1 parent 6a649fd commit 70e8630
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/dev/enjarai/minitardis/MiniTardis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,7 @@ public void onInitialize() {
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(interiorManager);

ModBlocks.load();
ModItems.load();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/dev/enjarai/minitardis/block/ModBlocks.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,22 +19,32 @@
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()));
public static final SimplePolymerBlock TARDIS_PLATING =
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<TardisExteriorBlockEntity> TARDIS_EXTERIOR_ENTITY =
registerEntity("tardis_exterior", TardisExteriorBlockEntity::new, TARDIS_EXTERIOR);
public static final BlockEntityType<ConsoleScreenBlockEntity> 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<? extends Block> ITEM_BLOCKS = List.of(TARDIS_PLATING, INTERIOR_DOOR, HANDBRAKE);


public static void load() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.Optional;

public interface TardisAwareBlock {
public interface TardisAware {
default Optional<Tardis> getTardis(World world) {
return world.getComponent(ModComponents.TARDIS_REFERENCE).getTardis();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TardisControl, Boolean> controlInput;

public ConsoleLeverBlock(Settings settings, BiConsumer<TardisControl, Boolean> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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 <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return validateTicker(type, ModBlocks.CONSOLE_SCREEN_ENTITY, (world1, pos, state1, blockEntity) -> blockEntity.tick(world1, pos, state1));
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> 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));
// }
}
Original file line number Diff line number Diff line change
@@ -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) {

}
}
4 changes: 4 additions & 0 deletions src/main/java/dev/enjarai/minitardis/component/Tardis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
20 changes: 20 additions & 0 deletions src/main/java/dev/enjarai/minitardis/item/ModItems.java
Original file line number Diff line number Diff line change
@@ -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));
}
});
}
}

0 comments on commit 70e8630

Please sign in to comment.