Skip to content

Commit

Permalink
writeable&copyable
Browse files Browse the repository at this point in the history
  • Loading branch information
IAFEnvoy committed Dec 31, 2024
1 parent 9f1d0cc commit 087a20d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.iafenvoy.sow.item.block;

import com.iafenvoy.sow.SongsOfWar;
import com.iafenvoy.sow.item.block.entity.WallsOfTimeBlockEntity;
import com.iafenvoy.sow.util.BookUtils;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
Expand All @@ -15,6 +14,7 @@
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.world.World;
import org.jetbrains.annotations.Nullable;

Expand All @@ -35,12 +35,29 @@ public BlockRenderType getRenderType(BlockState state) {
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ItemStack stack = player.getStackInHand(hand);
if (stack.isOf(Items.WRITTEN_BOOK)) {
NbtList nbtList = stack.getOrCreateNbt().getList("pages", 8);
if (nbtList != null) {
SongsOfWar.LOGGER.info(BookUtils.nbtToString(nbtList));
Direction direction = hit.getSide();
if (world.getBlockEntity(pos) instanceof WallsOfTimeBlockEntity blockEntity)
if (stack.isOf(Items.WRITTEN_BOOK) && direction.getAxis() != Direction.Axis.Y) {
NbtList nbtList = stack.getOrCreateNbt().getList("pages", 8);
if (nbtList != null) {
blockEntity.getContents().withContent(stack.copy()).withDirection(hit.getSide());
return ActionResult.SUCCESS;
}
} else if (stack.isOf(Items.WRITABLE_BOOK)) {
player.setStackInHand(hand, blockEntity.getContents().getContent().copy());
return ActionResult.SUCCESS;
}
}
return super.onUse(state, world, pos, player, hand, hit);
}

@Override
public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) {
super.onPlaced(world, pos, state, placer, itemStack);

}

@Override
public void afterBreak(World world, PlayerEntity player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
super.afterBreak(world, player, pos, state, blockEntity, tool);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

import com.iafenvoy.sow.SongsOfWar;
import com.iafenvoy.sow.registry.SowBlockEntities;
import com.iafenvoy.sow.util.BookUtils;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtOps;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;

import java.util.function.Supplier;

public class WallsOfTimeBlockEntity extends BlockEntity {
private WotContents contents = WotContents.EMPTY;
private WotContents contents = WotContents.EMPTY.get();

public WallsOfTimeBlockEntity(BlockPos pos, BlockState state) {
super(SowBlockEntities.WALLS_OF_TIME.get(), pos, state);
Expand All @@ -21,7 +26,7 @@ public WallsOfTimeBlockEntity(BlockPos pos, BlockState state) {
@Override
public void readNbt(NbtCompound nbt) {
super.readNbt(nbt);
this.contents = WotContents.CODEC.parse(NbtOps.INSTANCE, nbt.get("content")).resultOrPartial(SongsOfWar.LOGGER::error).orElse(WotContents.EMPTY);
this.contents = WotContents.CODEC.parse(NbtOps.INSTANCE, nbt.get("content")).resultOrPartial(SongsOfWar.LOGGER::error).orElse(WotContents.EMPTY.get());
}

@Override
Expand All @@ -35,23 +40,23 @@ public WotContents getContents() {
}

public static final class WotContents {
public static final WotContents EMPTY = new WotContents(Direction.UP, "", 0, 0, 0, 0);
public static final Supplier<WotContents> EMPTY = () -> new WotContents(Direction.UP, ItemStack.EMPTY, 0, 0, 1, 1);
public static final Codec<WotContents> CODEC = RecordCodecBuilder.create(i -> i.group(
Direction.CODEC.optionalFieldOf("direction", EMPTY.direction).forGetter(WotContents::getDirection),
Codec.STRING.optionalFieldOf("content", EMPTY.content).forGetter(WotContents::getContent),
Codec.INT.optionalFieldOf("offsetX", EMPTY.offsetX).forGetter(WotContents::getOffsetX),
Codec.INT.optionalFieldOf("offsetY", EMPTY.offsetY).forGetter(WotContents::getOffsetY),
Codec.INT.optionalFieldOf("sizeX", EMPTY.sizeX).forGetter(WotContents::getSizeX),
Codec.INT.optionalFieldOf("sizeY", EMPTY.sizeY).forGetter(WotContents::getSizeY)
Direction.CODEC.optionalFieldOf("direction", EMPTY.get().direction).forGetter(WotContents::getDirection),
ItemStack.CODEC.optionalFieldOf("content", EMPTY.get().content).forGetter(WotContents::getContent),
Codec.INT.optionalFieldOf("offsetX", EMPTY.get().offsetX).forGetter(WotContents::getOffsetX),
Codec.INT.optionalFieldOf("offsetY", EMPTY.get().offsetY).forGetter(WotContents::getOffsetY),
Codec.INT.optionalFieldOf("sizeX", EMPTY.get().sizeX).forGetter(WotContents::getSizeX),
Codec.INT.optionalFieldOf("sizeY", EMPTY.get().sizeY).forGetter(WotContents::getSizeY)
).apply(i, WotContents::new));
private Direction direction;
private String content;
private ItemStack content;
private int offsetX;
private int offsetY;
private int sizeX;
private int sizeY;

public WotContents(Direction direction, String content, int offsetX, int offsetY, int sizeX, int sizeY) {
public WotContents(Direction direction, ItemStack content, int offsetX, int offsetY, int sizeX, int sizeY) {
this.direction = direction;
this.content = content;
this.offsetX = offsetX;
Expand All @@ -64,7 +69,7 @@ public Direction getDirection() {
return this.direction;
}

public String getContent() {
public ItemStack getContent() {
return this.content;
}

Expand All @@ -89,7 +94,7 @@ public WotContents withDirection(Direction direction) {
return this;
}

public WotContents withContent(String content) {
public WotContents withContent(ItemStack content) {
this.content = content;
return this;
}
Expand All @@ -105,5 +110,12 @@ public WotContents withSize(int x, int y) {
this.sizeY = y;
return this;
}

public String getContentString() {
NbtList nbtList = this.content.getOrCreateNbt().getList("pages", 8);
if (nbtList != null)
return BookUtils.nbtToString(nbtList);
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.RotationAxis;

import java.util.List;

Expand All @@ -28,15 +29,14 @@ public WallsOfTimeBlockEntityRenderer(BlockEntityRendererFactory.Context ctx) {
@Override
public void render(WallsOfTimeBlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
matrices.push();
matrices.scale(0.03125F, 0.03125F, 0.03125F);
matrices.translate(0, 0, -0.1);
this.renderSingle("abcdefghijklmn", matrices, vertexConsumers, light);
matrices.pop();
}

private void renderSingle(String string, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light) {
List<OrderedText> texts = this.textRenderer.wrapLines(Text.literal(string).fillStyle(STYLE), 32);
WallsOfTimeBlockEntity.WotContents contents = entity.getContents();
matrices.translate(0.5, 0.5, 0.5);
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(180 - contents.getDirection().getHorizontal() * 90));
matrices.translate(0.5, 0.5, -0.501F);
matrices.scale(-0.03125F, -0.03125F, 0.03125F);
List<OrderedText> texts = this.textRenderer.wrapLines(Text.literal(contents.getContentString()).fillStyle(STYLE), contents.getSizeX() * 32);
for (int i = 0; i < texts.size(); i++)
this.textRenderer.draw(texts.get(i), 2, 2 + i * 10, 0x777777, false, matrices.peek().getPositionMatrix(), vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, light);
this.textRenderer.draw(texts.get(i), 2 + contents.getOffsetX() * 32, 2 + (int) (i * 10.6) + contents.getOffsetY() * 32, 0x777777, false, matrices.peek().getPositionMatrix(), vertexConsumers, TextRenderer.TextLayerType.NORMAL, 0, light);
matrices.pop();
}
}

0 comments on commit 087a20d

Please sign in to comment.