145 lines
5.4 KiB
Java
145 lines
5.4 KiB
Java
package com.thebrokenrail.slightlyvanilla.client.screen;
|
|
|
|
import com.mojang.blaze3d.systems.RenderSystem;
|
|
import com.thebrokenrail.slightlyvanilla.SlightlyVanilla;
|
|
import com.thebrokenrail.slightlyvanilla.mixin.ScreenHandlerAccessor;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.block.Blocks;
|
|
import net.minecraft.client.MinecraftClient;
|
|
import net.minecraft.client.gui.screen.Screen;
|
|
import net.minecraft.client.gui.widget.SliderWidget;
|
|
import net.minecraft.client.util.math.MatrixStack;
|
|
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
|
|
import net.minecraft.screen.ScreenHandlerContext;
|
|
import net.minecraft.state.property.Properties;
|
|
import net.minecraft.text.LiteralText;
|
|
import net.minecraft.text.TranslatableText;
|
|
import net.minecraft.util.Hand;
|
|
import net.minecraft.util.Identifier;
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.math.Direction;
|
|
import net.minecraft.util.math.Vec3d;
|
|
import net.minecraft.world.World;
|
|
|
|
import java.util.function.BiFunction;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class NoteBlockScreen extends Screen {
|
|
private static class NoteSliderWidget extends SliderWidget {
|
|
private final ScreenHandlerContext context;
|
|
|
|
public NoteSliderWidget(ScreenHandlerContext context, int x, int y, int width, int height) {
|
|
super(x, y, width, height, LiteralText.EMPTY, (double) getNote(context) / getMax());
|
|
this.context = context;
|
|
updateMessage();
|
|
}
|
|
|
|
private static double getMax() {
|
|
return (double) Properties.NOTE.getValues().size() - 1;
|
|
}
|
|
|
|
@SuppressWarnings("OptionalGetWithoutIsPresent")
|
|
private static int getNote(ScreenHandlerContext context) {
|
|
return context.run((BiFunction<World, BlockPos, Integer>) (world, pos) -> world.getBlockState(pos).get(Properties.NOTE)).get();
|
|
}
|
|
|
|
private int toNote() {
|
|
return (int) (value * getMax());
|
|
}
|
|
|
|
@Override
|
|
protected void updateMessage() {
|
|
int note = toNote();
|
|
setMessage(new TranslatableText("text." + SlightlyVanilla.NAMESPACE + ".noteblock_screen_slider", note, new TranslatableText("text." + SlightlyVanilla.NAMESPACE + ".noteblock_note." + note)));
|
|
}
|
|
|
|
@Override
|
|
protected void applyValue() {
|
|
context.run((world, blockPos) -> {
|
|
BlockState target = world.getBlockState(blockPos);
|
|
|
|
int currentNote = target.get(Properties.NOTE);
|
|
int targetNote = toNote();
|
|
|
|
int amount;
|
|
if (targetNote >= currentNote) {
|
|
amount = targetNote - currentNote;
|
|
} else {
|
|
amount = ((int) getMax()) - currentNote + 1 + targetNote;
|
|
}
|
|
|
|
MinecraftClient client = MinecraftClient.getInstance();
|
|
assert client != null;
|
|
assert client.getNetworkHandler() != null;
|
|
|
|
BlockHitResult hitResult = new BlockHitResult(Vec3d.ofCenter(blockPos), Direction.UP, blockPos, false);
|
|
for (int i = 0; i < amount; i++) {
|
|
client.getNetworkHandler().sendPacket(new PlayerInteractBlockC2SPacket(Hand.MAIN_HAND, hitResult));
|
|
}
|
|
|
|
world.setBlockState(blockPos, target.with(Properties.NOTE, targetNote));
|
|
});
|
|
}
|
|
}
|
|
|
|
private static final Identifier TEXTURE = new Identifier(SlightlyVanilla.NAMESPACE, "textures/gui/noteblock.png");
|
|
|
|
private final ScreenHandlerContext context;
|
|
|
|
public NoteBlockScreen(ScreenHandlerContext context) {
|
|
super(Blocks.NOTE_BLOCK.getName());
|
|
passEvents = true;
|
|
this.context = context;
|
|
}
|
|
|
|
@Override
|
|
protected void init() {
|
|
super.init();
|
|
int sliderWidth = BACKGROUND_WIDTH - 36;
|
|
int sliderHeight = 20;
|
|
addButton(new NoteSliderWidget(context, width / 2 - sliderWidth / 2, (height / 2) - (sliderHeight / 2) + 4, sliderWidth, sliderHeight));
|
|
}
|
|
|
|
private static final int BACKGROUND_WIDTH = 176;
|
|
private static final int BACKGROUND_HEIGHT = 60;
|
|
|
|
private static final int TITLE_X_OFFSET = 8;
|
|
private static final int TITLE_Y_OFFSEt = 6;
|
|
|
|
@Override
|
|
public void renderBackground(MatrixStack matrices) {
|
|
super.renderBackground(matrices);
|
|
|
|
//noinspection deprecation
|
|
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
|
|
assert client != null;
|
|
client.getTextureManager().bindTexture(TEXTURE);
|
|
int centerX = width / 2;
|
|
int centerY = height / 2;
|
|
drawTexture(matrices, centerX - (BACKGROUND_WIDTH / 2), centerY - (BACKGROUND_HEIGHT / 2), 0, 0, BACKGROUND_WIDTH, BACKGROUND_HEIGHT);
|
|
|
|
int titleX = centerX - (BACKGROUND_WIDTH / 2) + TITLE_X_OFFSET;
|
|
int titleY = centerY - (BACKGROUND_HEIGHT / 2) + TITLE_Y_OFFSEt;
|
|
textRenderer.draw(matrices, title, titleX, titleY, 4210752);
|
|
}
|
|
|
|
@Override
|
|
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
|
|
renderBackground(matrices);
|
|
super.render(matrices, mouseX, mouseY, delta);
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
super.tick();
|
|
assert client != null;
|
|
if (!ScreenHandlerAccessor.callCanUse(context, client.player, Blocks.NOTE_BLOCK)) {
|
|
assert client.player != null;
|
|
client.player.closeScreen();
|
|
}
|
|
}
|
|
}
|