Skip to content

Commit

Permalink
complete walls of time
Browse files Browse the repository at this point in the history
  • Loading branch information
IAFEnvoy committed Dec 31, 2024
1 parent 08b0b9e commit 8ecfb04
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 16 deletions.
7 changes: 6 additions & 1 deletion common/src/main/java/com/iafenvoy/sow/SongsOfWar.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.iafenvoy.sow.config.SowConfig;
import com.iafenvoy.sow.data.ArdoniName;
import com.iafenvoy.sow.data.BeaconData;
import com.iafenvoy.sow.item.block.entity.WallsOfTimeBlockEntity;
import com.iafenvoy.sow.network.ServerNetworkHelper;
import com.iafenvoy.sow.registry.*;
import com.iafenvoy.sow.registry.power.SowPowers;
Expand All @@ -13,9 +14,9 @@
import dev.architectury.event.events.common.BlockEvent;
import dev.architectury.registry.ReloadListenerRegistry;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.resource.ResourceType;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;

Expand Down Expand Up @@ -51,6 +52,10 @@ public static void process() {
BlockEvent.BREAK.register((world, pos, state, player, xp) -> {
if (state.isOf(Blocks.BEACON) && world instanceof ServerWorld serverWorld)
BeaconData.getInstance(serverWorld).remove(pos);
else if (world.getBlockEntity(pos) instanceof WallsOfTimeBlockEntity blockEntity && !blockEntity.getContents().getContent().isEmpty()) {
player.sendMessage(Text.translatable("block.sow.walls_of_time.cannot_break"));
return EventResult.interruptFalse();
}
return EventResult.pass();
});
ReloadListenerRegistry.register(ResourceType.SERVER_DATA, new ArdoniName(), Identifier.of(MOD_ID, "ardoni_name"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.iafenvoy.sow.item;

import com.iafenvoy.sow.item.block.entity.WallsOfTimeBlockEntity;
import com.iafenvoy.sow.registry.SowItemGroups;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Rarity;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class WallsOfTimeEditItem extends Item {
private static final String EDIT_TYPE_KEY = "edit_type";

public WallsOfTimeEditItem() {
super(new Settings().rarity(Rarity.EPIC).maxCount(1).arch$tab(SowItemGroups.ITEMS));
}

@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
super.inventoryTick(stack, world, entity, slot, selected);
if (!stack.getOrCreateNbt().contains(EDIT_TYPE_KEY))
stack.getOrCreateNbt().putString(EDIT_TYPE_KEY, WallsOfTimeBlockEntity.EditType.OFFSET_X_PLUS.name());
}

@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand);
if (user.isSneaking() && stack.getOrCreateNbt().contains(EDIT_TYPE_KEY)) {
WallsOfTimeBlockEntity.EditType next = WallsOfTimeBlockEntity.EditType.valueOf(stack.getOrCreateNbt().getString(EDIT_TYPE_KEY)).next();
stack.getOrCreateNbt().putString(EDIT_TYPE_KEY, next.name());
user.sendMessage(Text.translatable("item.sow.walls_of_time_edit.mode", next.name()), true);
return TypedActionResult.success(stack);
}
return super.use(world, user, hand);
}

@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);
tooltip.add(Text.translatable("item.sow.walls_of_time_edit.mode", stack.getOrCreateNbt().getString(EDIT_TYPE_KEY)));
}

@Override
public ActionResult useOnBlock(ItemUsageContext context) {
if (context.getWorld().getBlockEntity(context.getBlockPos()) instanceof WallsOfTimeBlockEntity blockEntity) {
WallsOfTimeBlockEntity.EditType.valueOf(context.getStack().getOrCreateNbt().getString(EDIT_TYPE_KEY)).getProcess().accept(blockEntity.getContents());
blockEntity.sync();
return ActionResult.SUCCESS;
}
return super.useOnBlock(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
NbtList nbtList = stack.getOrCreateNbt().getList("pages", 8);
if (nbtList != null) {
blockEntity.getContents().withContent(stack.copy()).withDirection(hit.getSide());
blockEntity.sync();
return ActionResult.SUCCESS;
}
} else if (stack.isOf(Items.WRITABLE_BOOK)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.iafenvoy.sow.item.block.entity;

import com.iafenvoy.neptune.ServerHelper;
import com.iafenvoy.neptune.network.ClientNetworkHelper;
import com.iafenvoy.neptune.network.ServerNetworkHelper;
import com.iafenvoy.sow.SongsOfWar;
import com.iafenvoy.sow.registry.SowBlockEntities;
import com.iafenvoy.sow.util.BookUtils;
Expand All @@ -11,13 +14,16 @@
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtOps;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;

import java.util.function.Consumer;
import java.util.function.Supplier;

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

public WallsOfTimeBlockEntity(BlockPos pos, BlockState state) {
super(SowBlockEntities.WALLS_OF_TIME.get(), pos, state);
Expand All @@ -27,6 +33,7 @@ public WallsOfTimeBlockEntity(BlockPos pos, BlockState state) {
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.get());
this.fulfilled = true;
}

@Override
Expand All @@ -36,11 +43,18 @@ protected void writeNbt(NbtCompound nbt) {
}

public WotContents getContents() {
if (!this.fulfilled) ClientNetworkHelper.requestBlockEntityData(this.pos);
return this.contents;
}

public void sync() {
if (this.world != null && this.world.isClient || ServerHelper.server == null) return;
for (ServerPlayerEntity player : ServerHelper.server.getPlayerManager().getPlayerList())
ServerNetworkHelper.sendBlockEntityData(player, this.pos, this);
}

public static final class WotContents {
public static final Supplier<WotContents> EMPTY = () -> new WotContents(Direction.UP, ItemStack.EMPTY, 0, 0, 1, 1);
public static final Supplier<WotContents> EMPTY = () -> new WotContents(Direction.EAST, ItemStack.EMPTY, 0, 0, 1, 1);
public static final Codec<WotContents> CODEC = RecordCodecBuilder.create(i -> i.group(
Direction.CODEC.optionalFieldOf("direction", EMPTY.get().direction).forGetter(WotContents::getDirection),
ItemStack.CODEC.optionalFieldOf("content", EMPTY.get().content).forGetter(WotContents::getContent),
Expand Down Expand Up @@ -99,23 +113,37 @@ public WotContents withContent(ItemStack content) {
return this;
}

public WotContents withOffset(int x, int y) {
this.offsetX = x;
this.offsetY = y;
return this;
}

public WotContents withSize(int x, int y) {
this.sizeX = x;
this.sizeY = y;
return this;
}

public String getContentString() {
NbtList nbtList = this.content.getOrCreateNbt().getList("pages", 8);
if (nbtList != null)
return BookUtils.nbtToString(nbtList);
return "";
}
}

public enum EditType {
OFFSET_X_PLUS(c -> c.offsetX++),
OFFSET_X_SUB(c -> c.offsetX--),
OFFSET_Y_PLUS(c -> c.offsetY++),
OFFSET_Y_SUB(c -> c.offsetY--),
SIZE_X_PLUS(c -> c.sizeX++),
SIZE_X_SUB(c -> c.sizeX--),
SIZE_Y_PLUS(c -> c.sizeY++),
SIZE_Y_SUB(c -> c.sizeY--),
CLEAR(c -> c.content = ItemStack.EMPTY);

private final Consumer<WotContents> process;

EditType(Consumer<WotContents> process) {
this.process = process;
}

public Consumer<WotContents> getProcess() {
return this.process;
}

public EditType next() {
return values()[(this.ordinal() + 1) % values().length];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class SowItems {
public static final RegistrySupplier<Item> GREEN_NOTE = REGISTRY.register("green_note", () -> new NoteItem(PowerCategory.SUPPORTIUM));

public static final RegistrySupplier<ShrineDebugItem> SHRINE_DEBUG = REGISTRY.register("shrine_debug", ShrineDebugItem::new);
public static final RegistrySupplier<WallsOfTimeEditItem> WALLS_OF_TIME_EDIT = REGISTRY.register("walls_of_time_edit", WallsOfTimeEditItem::new);
//Fake Items, should not use in game without song power.
public static final RegistrySupplier<Item> PROTEPOINT_SHIELD = REGISTRY.register("protepoint_shield", ProtepointShieldItem::create);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void render(WallsOfTimeBlockEntity entity, float tickDelta, MatrixStack m
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++)
List<OrderedText> texts = this.textRenderer.wrapLines(Text.literal(contents.getContentString()).fillStyle(STYLE), Math.max(0, contents.getSizeX()) * 32);
for (int i = 0; i < texts.size() && i < contents.getSizeY() * 3; i++)
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();
}
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/resources/assets/sow/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"block.sow.val_head": "Val Head",
"block.sow.vulcannus_head": "Vulcannus Head",
"block.sow.walls_of_time": "Walls Of Time",
"block.sow.walls_of_time.cannot_break": "You need to clear all contents before break this block!",
"block.sow.wooden_food_plate": "Wooden Food Plate",
"block.sow.xaria_head": "Xaria Head",
"block.sow.yujuki_head": "Yujuki Head",
Expand Down Expand Up @@ -567,6 +568,8 @@
"item.sow.voltaris_ardoni_spawn_egg": "Voltaris Ardoni Spawn Egg",
"item.sow.vulcannus_spawn_egg": "Vulcannus Spawn Egg",
"item.sow.walls_of_time": "Walls Of Time",
"item.sow.walls_of_time_edit": "Walls of Time Edit Tool",
"item.sow.walls_of_time_edit.mode": "Current mode: %s",
"item.sow.wither_staff": "Wither Staff",
"item.sow.xaria_spawn_egg": "Xaria Spawn Egg",
"item.sow.yellow_note": "Yellow Note",
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/resources/assets/sow/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"block.sow.val_head": "瓦尔的头",
"block.sow.vulcannus_head": "瓦坎的头",
"block.sow.walls_of_time": "时间之墙",
"block.sow.walls_of_time.cannot_break": "在破坏这个方块前需要删除其中的内容!",
"block.sow.wooden_food_plate": "木制盘子",
"block.sow.xaria_head": "泽里亚的头",
"block.sow.yujuki_head": "柚希的头",
Expand Down Expand Up @@ -567,6 +568,8 @@
"item.sow.voltaris_ardoni_spawn_egg": "伏尔塔里斯族阿多尼人刷怪蛋",
"item.sow.vulcannus_spawn_egg": "瓦坎刷怪蛋",
"item.sow.walls_of_time": "时间之墙",
"item.sow.walls_of_time_edit": "时间之墙编辑工具",
"item.sow.walls_of_time_edit.mode": "当前模式:%s",
"item.sow.wither_staff": "凋零法杖",
"item.sow.xaria_spawn_egg": "泽里亚刷怪蛋",
"item.sow.yellow_note": "黄色音符",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/handheld",
"textures": {
"layer0": "minecraft:item/stick"
}
}

0 comments on commit 8ecfb04

Please sign in to comment.