Skip to content

Commit

Permalink
Add: the gui of delayer.
Browse files Browse the repository at this point in the history
Bump version to 1.4.36

Took 1 hour 22 minutes
  • Loading branch information
xkball committed Dec 2, 2024
1 parent 9e51812 commit aa08522
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod_name=Power Tool
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPL-3.0
# The mod version. See https://semver.org/
mod_version=1.4.35
mod_version=1.4.36
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/teacon/powertool/block/RedStoneDelayBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
Expand All @@ -25,8 +29,11 @@
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.Nullable;
import org.teacon.powertool.block.entity.RedStoneDelayBlockEntity;
import org.teacon.powertool.network.client.OpenBlockScreen;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
Expand Down Expand Up @@ -81,14 +88,22 @@ public boolean hasAnalogOutputSignal(BlockState state) {
protected int getAnalogOutputSignal(BlockState state, Level level, BlockPos pos) {
var be = level.getBlockEntity(pos);
if(!(be instanceof RedStoneDelayBlockEntity te)) return 0;
return (int) Mth.clamp((te.delayTicks - te.delayedTicks)/(float)te.delayTicks,0f,15f);
return (int) Mth.clamp(((te.delayTicks - te.delayedTicks)/(float)te.delayTicks)*15,0f,15f);
}

@Override
protected boolean isSignalSource(BlockState state) {
return true;
}

@Override
protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if(!level.isClientSide() && player.getAbilities().instabuild && player instanceof ServerPlayer serverPlayer) {
PacketDistributor.sendToPlayer(serverPlayer,new OpenBlockScreen(pos,OpenBlockScreen.RED_STONE_DELAYER));
}
return ItemInteractionResult.SUCCESS;
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class RedStoneDelayBlockEntity extends BlockEntity {
public class RedStoneDelayBlockEntity extends BlockEntity implements IClientUpdateBlockEntity{

public int delayTicks;
public int delayedTicks;
public boolean counting;
public boolean checkRisingEdge;
public Mode mode = Mode.BLOCK;
public Mode mode = Mode.IGNORE;
public Boolean powered;
public Boolean poweredOld;

Expand All @@ -46,11 +46,12 @@ public static void tick(Level level, BlockPos pos, BlockState state, RedStoneDel
}
if(risingEdge != null && blockEntity.checkRisingEdge == risingEdge){
blockEntity.counting = true;
if(blockEntity.mode == Mode.OVERWRITE) blockEntity.delayedTicks = 0;
if(blockEntity.mode == Mode.RESET) blockEntity.delayedTicks = 0;
blockEntity.setChanged();
}
if(blockEntity.counting){
blockEntity.delayedTicks++;
if(blockEntity.delayedTicks%2==0) level.updateNeighbourForOutputSignal(pos,state.getBlock());
}
if(blockEntity.counting && blockEntity.delayedTicks >= blockEntity.delayTicks){
blockEntity.delayedTicks = 0;
Expand All @@ -62,14 +63,18 @@ public static void tick(Level level, BlockPos pos, BlockState state, RedStoneDel

}

public void read(CompoundTag tag) {
public void readWithOutState(CompoundTag tag) {
if(tag.contains("DelayTicks")) this.delayTicks = tag.getInt("DelayTicks");
if(tag.contains("DelayedTicks")) this.delayedTicks = tag.getInt("DelayedTicks");
if(tag.contains("Mode")) this.mode = Mode.fromId(tag.getInt("Mode"));
if(tag.contains("counting")) this.counting = tag.getBoolean("counting");
if(tag.contains("checkRisingEdge")) this.checkRisingEdge = tag.getBoolean("checkRisingEdge");
}

public void read(CompoundTag tag) {
readWithOutState(tag);
if(tag.contains("DelayedTicks")) this.delayedTicks = tag.getInt("DelayedTicks");
if(tag.contains("counting")) this.counting = tag.getBoolean("counting");
}

public CompoundTag write(CompoundTag tag) {
tag.putInt("DelayTicks", this.delayTicks);
tag.putInt("DelayedTicks", this.delayedTicks);
Expand Down Expand Up @@ -116,12 +121,22 @@ public void onDataPacket(Connection net, ClientboundBlockEntityDataPacket pkt, H
this.handleUpdateTag(pkt.getTag(),lookupProvider);
}

@Override
public void update(CompoundTag tag, HolderLookup.Provider registries) {
readWithOutState(tag);
}

@Override
public void writeToPacket(CompoundTag tag, HolderLookup.Provider registries) {
write(tag);
}

public enum Mode{
BLOCK,
OVERWRITE;
IGNORE,
RESET;

public static Mode fromId(int id) {
return id == 0 ? BLOCK : OVERWRITE;
return id == 0 ? IGNORE : RESET;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.teacon.powertool.client.gui;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.SpriteIconButton;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.PacketDistributor;
import org.teacon.powertool.block.entity.RedStoneDelayBlockEntity;
import org.teacon.powertool.client.gui.widget.ObjectInputBox;
import org.teacon.powertool.network.server.UpdateBlockEntityData;
import org.teacon.powertool.utils.VanillaUtils;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Objects;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
public class RedStoneDelayerScreen extends Screen {

private static final ResourceLocation TEXTURE_RISING_EDGE = VanillaUtils.modRL("delayer_button_rising_edge");
private static final ResourceLocation TEXTURE_DESCENDING_EDGE = VanillaUtils.modRL("delayer_button_descending_edge");
private static final ResourceLocation TEXTURE_IGNORE = VanillaUtils.modRL("delayer_button_ignore");
private static final ResourceLocation TEXTURE_RESET = VanillaUtils.modRL("delayer_button_reset");

protected final RedStoneDelayBlockEntity te;
protected ObjectInputBox<Integer> delayInput;
protected RedStoneDelayBlockEntity.Mode mode;
protected boolean checkRisingEdge;

public RedStoneDelayerScreen(RedStoneDelayBlockEntity te) {
super(Component.literal("delayer"));
this.te = te;
}

@Override
protected void init() {
var mc = Minecraft.getInstance();
var font = mc.font;
var box_l = (int)Math.max(100,width*0.2);
var startY = (int)(height*0.15);
mode = te.mode;
checkRisingEdge = te.checkRisingEdge;

this.addRenderableWidget(new Button.Builder(CommonComponents.GUI_DONE, btn -> this.onDone())
.pos(this.width / 2 - 100, this.height / 2 + startY)
.size(200, 20).build());

this.delayInput = new ObjectInputBox<>(font,this.width/2 - box_l/2,height/2,box_l,20,Component.literal("Delay Ticks"),ObjectInputBox.INT_VALIDATOR,ObjectInputBox.INT_RESPONDER);
this.delayInput.setMaxLength(6);
this.delayInput.setValue(String.valueOf(te.delayTicks));

var buttonCheckRisingEdge = new SpriteIconButton.Builder(Component.empty(),(btn) -> checkRisingEdge = true,true)
.sprite(TEXTURE_RISING_EDGE,16,16)
.size(20, 20)
.build();
buttonCheckRisingEdge.setPosition(this.width/2 - 22,height/2 - 50);
buttonCheckRisingEdge.setTooltip(Tooltip.create(Component.translatable("powertool.gui.delayer.check_rising_edge")));
var buttonCheckDescendingEdge = new SpriteIconButton.Builder(Component.empty(),(btn) -> checkRisingEdge = false,true)
.sprite(TEXTURE_DESCENDING_EDGE,16,16)
.size(20, 20)
.build();
buttonCheckDescendingEdge.setPosition(this.width/2+3,height/2 - 50);
buttonCheckDescendingEdge.setTooltip(Tooltip.create(Component.translatable("powertool.gui.delayer.check_descending_edge")));
var buttonIgnore = new SpriteIconButton.Builder(Component.empty(),(btn) -> mode = RedStoneDelayBlockEntity.Mode.IGNORE,true)
.sprite(TEXTURE_IGNORE,16,16)
.size(20, 20)
.build();
buttonIgnore.setPosition(this.width/2 - 22,height/2 - 25);
buttonIgnore.setTooltip(Tooltip.create(Component.translatable("powertool.gui.delayer.ignore")));
var buttonReset = new SpriteIconButton.Builder(Component.empty(),(btn) -> mode = RedStoneDelayBlockEntity.Mode.RESET,true)
.sprite(TEXTURE_RESET,16,16)
.size(20, 20)
.build();
buttonReset.setPosition(this.width/2+3,height/2 - 25);
buttonReset.setTooltip(Tooltip.create(Component.translatable("powertool.gui.delayer.reset")));
this.addRenderableWidget(this.delayInput);
this.addRenderableWidget(buttonCheckRisingEdge);
this.addRenderableWidget(buttonCheckDescendingEdge);
this.addRenderableWidget(buttonIgnore);
this.addRenderableWidget(buttonReset);
super.init();
}

protected void onDone() {
if (this.minecraft != null) {
this.minecraft.setScreen(null);
}
}

@Override
public void removed() {
if(this.delayInput == null) return;
te.delayTicks = Objects.requireNonNullElse(delayInput.get(),0);
te.mode = mode;
te.checkRisingEdge = checkRisingEdge;
PacketDistributor.sendToServer(UpdateBlockEntityData.create(te));
}

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
super.render(guiGraphics, mouseX, mouseY, partialTick);
var currentMode1 = Component.translatable("powertool.gui.delayer.mode1");
var currentMode2 = Component.translatable("powertool.gui.delayer.mode2");
guiGraphics.drawString(font,currentMode1,width/2 - 50 - font.width(currentMode1),height/2 - 50 + 6,-1);
guiGraphics.drawString(font,currentMode2,width/2 - 50 - font.width(currentMode2),height/2 - 25 + 6,-1);
guiGraphics.blitSprite(mode == RedStoneDelayBlockEntity.Mode.IGNORE ? TEXTURE_IGNORE : TEXTURE_RESET,width/2 - 43,height/2 - 25 + 2,16,16);
guiGraphics.blitSprite(checkRisingEdge? TEXTURE_RISING_EDGE : TEXTURE_DESCENDING_EDGE,width/2 - 43,height/2 - 50 + 2,16,16);
}

@Override
public boolean isPauseScreen() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import org.teacon.powertool.block.entity.RedStoneDelayBlockEntity;
import org.teacon.powertool.block.entity.TimeObserverBlockEntity;
import org.teacon.powertool.client.gui.RedStoneDelayerScreen;
import org.teacon.powertool.client.gui.observers.GameTimeCycleObserverScreen;
import org.teacon.powertool.client.gui.observers.RealTimeCycleObserverScreen;
import org.teacon.powertool.client.gui.observers.RealTimeObserverScreen;
Expand All @@ -20,6 +22,7 @@ public record OpenBlockScreen(BlockPos pos, int guiType) implements CustomPacket
public static final int REAL_TIME_OBSERVER = 1;
public static final int REAL_TIME_CYCLE_OBSERVER = 2;
public static final int GAME_TIME_CYCLE_OBSERVER = 3;
public static final int RED_STONE_DELAYER = 4;

public static final CustomPacketPayload.Type<OpenBlockScreen> TYPE = new Type<>(VanillaUtils.modRL("open_gui"));

Expand Down Expand Up @@ -54,6 +57,9 @@ else if(pack.guiType == REAL_TIME_CYCLE_OBSERVER && te instanceof TimeObserverBl
else if(pack.guiType == GAME_TIME_CYCLE_OBSERVER && te instanceof TimeObserverBlockEntity _te){
Minecraft.getInstance().setScreen(new GameTimeCycleObserverScreen(_te));
}
else if(pack.guiType == RED_STONE_DELAYER && te instanceof RedStoneDelayBlockEntity _te){
Minecraft.getInstance().setScreen(new RedStoneDelayerScreen(_te));
}
}
}
}
7 changes: 7 additions & 0 deletions src/main/resources/assets/powertool/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"block.powertool.observer_realtime": "Realtime Observer",
"block.powertool.observer_realtime_cyl": "Realtime Cycle Observer",
"block.powertool.observer_gametime_cyl": "Gametime Cycle Observer",
"block.powertool.delayer": "Delayer",
"item.powertool.useless_stick": "Seemingly Useless Stick",
"item.powertool.clap": "Clap",
"item.powertool.clap_but_sad": "Sad Clap",
Expand Down Expand Up @@ -118,6 +119,12 @@
"powertool.gui.holographic_sign.lock.true": "render direction locked",
"powertool.gui.holographic_sign.lock.false": "render direction follows player",
"powertool.gui.register.match_data": "Match Data",
"powertool.gui.delayer.check_rising_edge": "Trigger delay on rising edge.",
"powertool.gui.delayer.check_descending_edge": "Trigger delay on descending edge.",
"powertool.gui.delayer.ignore": "Ignore when triggered during delay.",
"powertool.gui.delayer.reset": "Reset delay when triggered during delay",
"powertool.gui.delayer.mode1": "Current Trigger Mode: ",
"powertool.gui.delayer.mode2": "Current Recursion Trigger Mode: ",

"powertool.command.fly.enabled": "Flying enabled",
"powertool.command.fly.disabled": "Flying disabled",
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/assets/powertool/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
"powertool.gui.holographic_sign.lock.true": "方向锁定:开启",
"powertool.gui.holographic_sign.lock.false": "方向锁定:关闭",
"powertool.gui.register.match_data": "精确匹配物品数据",
"powertool.gui.delayer.check_rising_edge": "上升沿触发延时",
"powertool.gui.delayer.check_descending_edge": "下降沿触发延时",
"powertool.gui.delayer.ignore": "延时过程中无视触发信号",
"powertool.gui.delayer.reset": "延时过程中接收到触发信号将重置延时",
"powertool.gui.delayer.mode1": "当前触发模式: ",
"powertool.gui.delayer.mode2": "当前递归触发模式: ",

"powertool.command.fly.enabled": "创造飞行已开启。",
"powertool.command.fly.disabled": "创造飞行已关闭",
Expand All @@ -113,6 +119,7 @@
"block.powertool.observer_realtime": "现实世界侦测器",
"block.powertool.observer_realtime_cyl": "现实日循环侦测器",
"block.powertool.observer_gametime_cyl": "游戏日循环侦测器",
"block.powertool.delayer": "延时器",
"item.powertool.examine_holo_glass": "全息透视眼镜",
"item.powertool.command_rune": "命令碎片",
"item.powertool.tonk": "\"绳拴\"",
Expand Down

0 comments on commit aa08522

Please sign in to comment.