Skip to content

Commit

Permalink
Hm quicksave
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Nov 18, 2023
1 parent d5c82af commit a9d49d3
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

import dev.enjarai.minitardis.block.ModBlocks;
import dev.enjarai.minitardis.block.TardisAware;
import dev.enjarai.minitardis.item.PolymerModels;
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.ItemDisplayElement;
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.entity.decoration.Brightness;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvents;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.RotationAxis;
Expand All @@ -25,7 +33,7 @@
import org.jetbrains.annotations.Nullable;

@SuppressWarnings("deprecation")
public class ConsoleScreenBlock extends BlockWithEntity implements PolymerBlock, TardisAware, BlockWithElementHolder {
public class ConsoleScreenBlock extends BlockWithEntity implements PolymerBlock, TardisAware, BlockWithElementHolder, ConsoleInput {
public static final DirectionProperty FACING = HorizontalFacingBlock.FACING;

public ConsoleScreenBlock(Settings settings) {
Expand Down Expand Up @@ -59,6 +67,23 @@ public BlockState getPlacementState(ItemPlacementContext ctx) {
return null;
}

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
var facing = state.get(FACING);
var hitSide = hit.getSide();

if (hitSide == facing.rotateYClockwise() || hitSide == facing.rotateYCounterclockwise()) {
var newPos = pos.offset(hitSide).offset(facing.getOpposite());
world.setBlockState(newPos, state.with(FACING, hitSide));
// TODO move BE data here
world.setBlockState(pos, Blocks.AIR.getDefaultState());
inputSuccess(world, newPos, SoundEvents.BLOCK_IRON_TRAPDOOR_CLOSE, 0);
return ActionResult.SUCCESS;
}

return super.onUse(state, world, pos, player, hand, hit);
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
Expand All @@ -81,53 +106,30 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt

@Override
public Block getPolymerBlock(BlockState state) {
return Blocks.LIGHT;
return Blocks.BARRIER;
}

@Override
public BlockState getPolymerBlockState(BlockState state) {
return getPolymerBlock(state).getDefaultState().with(LightBlock.LEVEL_15, 3);
}
// @Override
// public BlockState getPolymerBlockState(BlockState state) {
// return getPolymerBlock(state).getDefaultState().with(LightBlock.LEVEL_15, 3);
// }

// @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 rotation = RotationAxis.NEGATIVE_Y.rotationDegrees(initialBlockState.get(FACING).asRotation());
//
// var stateText = new TextDisplayElement();
// var destinationText = new TextDisplayElement();
//
// stateText.setRightRotation(rotation);
// destinationText.setRightRotation(rotation);
// destinationText.setOffset(new Vec3d(0, -0.5, 0));
//
// return new ElementHolder() {
// {
// addElement(stateText);
// addElement(destinationText);
// update();
// }
//
// @Override
// protected void onTick() {
// update();
// }
//
// private void update() {
// stateText.setText(tardis.getState().getName());
// destinationText.setText(Text.literal(tardis.getDestination()
// .map(l -> l.pos().getX() + " " + l.pos().getY() + " " + l.pos().getZ() + " " + l.facing().getName().toUpperCase().charAt(0))
// .orElse("Unknown"))); // TODO dimension
// }
// };
// }
// return null;
// }
@Override
public @Nullable ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) {
var facing = initialBlockState.get(FACING);

var exteriorElement = new ItemDisplayElement();
exteriorElement.setItem(PolymerModels.getStack(PolymerModels.ROTATING_MONITOR));
exteriorElement.setOffset(Vec3d.ZERO.offset(facing.getOpposite(), 0.495));
exteriorElement.setRightRotation(RotationAxis.NEGATIVE_Y.rotationDegrees(facing.asRotation()));

return new ElementHolder() {{
addElement(exteriorElement);
}};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public class ConsoleScreenBlockEntity extends BlockEntity implements TardisAware

public ConsoleScreenBlockEntity(BlockPos pos, BlockState state) {
super(ModBlocks.CONSOLE_SCREEN_ENTITY, pos, state);
var facing = state.get(ConsoleScreenBlock.FACING);
this.display = VirtualDisplay
.builder(DrawableCanvas.create(), pos, state.get(ConsoleScreenBlock.FACING))
.builder(DrawableCanvas.create(), pos.offset(facing), facing)
.glowing()
.invisible()
.build();
Expand Down Expand Up @@ -102,10 +103,15 @@ private void refresh(Tardis tardis) {
switch (value) {
case 0 -> CanvasColor.BLACK_HIGH;
case 1 -> CanvasColor.DEEPSLATE_GRAY_HIGH;
default -> CanvasColor.BLUE_NORMAL;
case 2 -> CanvasColor.BLUE_NORMAL;
case 3 -> CanvasColor.LIGHT_BLUE_NORMAL;
case 4 -> CanvasColor.ORANGE_LOWEST;
default -> CanvasColor.WHITE_HIGH;
});
}
}
canvas.set(DestinationScanner.RANGE / 2, 16 + DestinationScanner.RANGE / 2, CanvasColor.ORANGE_HIGH);
canvas.set(DestinationScanner.RANGE / 2, 16 + DestinationScanner.RANGE / 2 - 1, CanvasColor.ORANGE_HIGH);

var destination = tardis.getDestination();
DefaultFonts.VANILLA.drawText(canvas,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.enjarai.minitardis.component;

import dev.enjarai.minitardis.block.ModBlocks;
import net.minecraft.block.BlockState;
import net.minecraft.fluid.Fluids;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import org.joml.Vector2i;
Expand Down Expand Up @@ -58,13 +60,21 @@ private Iterator<Vector2i> updateAxis(ServerWorld world, byte[] axis, Iterator<V
pos2.add(RANGE / 2 - 1, RANGE / 2 - 1);

var state = world.getBlockState(pos3);
byte value = (byte) (state.isReplaceable() ? 0 : state.isIn(ModBlocks.TARDIS_EXTERIOR_PARTS) ? 2 : 1);
byte value = getValue(state);
axis[getIndex(pos2)] = value;
}

return iterator;
}

private byte getValue(BlockState state) {
if (state.getFluidState().isOf(Fluids.WATER)) return 3;
if (state.getFluidState().isOf(Fluids.LAVA)) return 4;
if (state.isReplaceable()) return 0;
if (state.isIn(ModBlocks.TARDIS_EXTERIOR_PARTS)) return 2;
return 1;
}

public static int getIndex(Vector2i pos) {
return getIndex(pos.x, pos.y);
}
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/dev/enjarai/minitardis/component/TardisControl.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
package dev.enjarai.minitardis.component;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.enjarai.minitardis.component.flight.LandingState;
import dev.enjarai.minitardis.component.flight.SearchingForLandingState;
import dev.enjarai.minitardis.component.flight.TakingOffState;
import dev.enjarai.minitardis.component.screen.ScreenApp;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;

import java.util.Optional;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
* Takes in button presses and other inputs from the Tardis console and translates them into actions performed on it.
*/
public class TardisControl {
public static final Codec<TardisControl> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("coordinate_scale").forGetter(c -> c.coordinateScale)
Codec.INT.fieldOf("coordinate_scale").forGetter(c -> c.coordinateScale),
ScreenApp.CODEC.listOf().fieldOf("screen_apps").forGetter(c -> ImmutableList.copyOf(c.screenApps.values()))
).apply(instance, TardisControl::new));

private int coordinateScale;
private final Map<Identifier, ScreenApp> screenApps;

Tardis tardis;

private TardisControl(int coordinateScale) {
private TardisControl(int coordinateScale, Collection<ScreenApp> screenApps) {
this.coordinateScale = coordinateScale;
var builder = ImmutableMap.<Identifier, ScreenApp>builder();
ScreenApp.CONSTRUCTORS.forEach((key, value) -> builder.put(key, value.get()));
screenApps.forEach(app -> builder.put(app.id(), app));
this.screenApps = builder.buildKeepingLast();
}

@SuppressWarnings("CopyConstructorMissesField")
public TardisControl(TardisControl copyFrom) {
this(copyFrom.coordinateScale);
this(copyFrom.coordinateScale, copyFrom.screenApps.values());
}

public TardisControl() {
this(1);
this(1, List.of());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.enjarai.minitardis.component.screen;

import com.mojang.serialization.Codec;
import eu.pb4.mapcanvas.api.core.DrawableCanvas;
import net.minecraft.util.Identifier;

import java.util.Map;
import java.util.function.Supplier;

public interface ScreenApp {
Map<Identifier, Codec<? extends ScreenApp>> ALL = Map.of(

);
Map<Identifier, Supplier<? extends ScreenApp>> CONSTRUCTORS = Map.of(

);
Codec<ScreenApp> CODEC = Identifier.CODEC.dispatch(ScreenApp::id, ALL::get);

void draw(DrawableCanvas canvas);

boolean onClick(int x, int y);

Identifier id();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class PolymerModels {
public static final PolymerModelData TARDIS = get("item/tardis");
public static final PolymerModelData INTERIOR_DOOR = get("item/interior_door");
public static final PolymerModelData ROTATING_MONITOR = get("item/rotating_monitor");

private static PolymerModelData get(String modelPath) {
return PolymerResourcePackUtils.requestModel(Items.LAPIS_LAZULI, MiniTardis.id(modelPath));
Expand Down

0 comments on commit a9d49d3

Please sign in to comment.