Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@

import com.direwolf20.justdirethings.client.screens.basescreens.BaseMachineScreen;
import com.direwolf20.justdirethings.client.screens.standardbuttons.ToggleButtonFactory;
import com.direwolf20.justdirethings.client.screens.widgets.GrayscaleButton;
import com.direwolf20.justdirethings.client.screens.widgets.ToggleButton;
import com.direwolf20.justdirethings.common.blockentities.BlockBreakerT1BE;
import com.direwolf20.justdirethings.common.containers.BlockBreakerT1Container;
import com.direwolf20.justdirethings.common.network.data.BreakerPayload;
import com.direwolf20.justdirethings.util.MiscHelpers;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.network.PacketDistributor;

public class BlockBreakerT1Screen extends BaseMachineScreen<BlockBreakerT1Container> {
public boolean sneaking;

public BlockBreakerT1Screen(BlockBreakerT1Container container, Inventory inv, Component name) {
super(container, inv, name);
if (baseMachineBE instanceof BlockBreakerT1BE breaker) {
sneaking = breaker.sneaking;
}
}

@Override
public void init() {
super.init();

addRenderableWidget(ToggleButtonFactory.SNEAKCLICKBUTTON(getGuiLeft() + 56, topSectionTop + 38, sneaking, b -> {
sneaking = !sneaking;
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
}

@Override
Expand All @@ -43,4 +58,10 @@ protected void drawMachineSlot(GuiGraphics guiGraphics, Slot slot) {
else
super.drawMachineSlot(guiGraphics, slot);
}

@Override
public void saveSettings() {
super.saveSettings();
PacketDistributor.sendToServer(new BreakerPayload(sneaking));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.direwolf20.justdirethings.client.screens.basescreens.BaseMachineScreen;
import com.direwolf20.justdirethings.client.screens.standardbuttons.ToggleButtonFactory;
import com.direwolf20.justdirethings.client.screens.widgets.GrayscaleButton;
import com.direwolf20.justdirethings.client.screens.widgets.ToggleButton;
import com.direwolf20.justdirethings.common.blockentities.BlockBreakerT1BE;
import com.direwolf20.justdirethings.common.containers.BlockBreakerT2Container;
import com.direwolf20.justdirethings.common.network.data.BreakerPayload;
import com.direwolf20.justdirethings.common.network.data.DirectionSettingPayload;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Component;
Expand All @@ -13,8 +16,13 @@
import net.neoforged.neoforge.network.PacketDistributor;

public class BlockBreakerT2Screen extends BaseMachineScreen<BlockBreakerT2Container> {
public boolean sneaking;

public BlockBreakerT2Screen(BlockBreakerT2Container container, Inventory inv, Component name) {
super(container, inv, name);
if (baseMachineBE instanceof BlockBreakerT1BE breaker) {
sneaking = breaker.sneaking;
}
}

@Override
Expand All @@ -30,6 +38,11 @@ public void init() {
direction = ((ToggleButton) b).getTexturePosition();
PacketDistributor.sendToServer(new DirectionSettingPayload(direction));
}));
addRenderableWidget(ToggleButtonFactory.SNEAKCLICKBUTTON(getGuiLeft() + 8, topSectionTop + 44, sneaking, b -> {
sneaking = !sneaking;
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
}

@Override
Expand All @@ -40,4 +53,10 @@ protected void drawMachineSlot(GuiGraphics guiGraphics, Slot slot) {
else
super.drawMachineSlot(guiGraphics, slot);
}

@Override
public void saveSettings() {
super.saveSettings();
PacketDistributor.sendToServer(new BreakerPayload(sneaking));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -41,6 +42,7 @@ public BlockBreakingProgress(BlockState blockState, int ticks, int iterator, flo
LinkedHashMap<BlockPos, BlockBreakingProgress> blockBreakingTracker = new LinkedHashMap<>();
public RedstoneControlData redstoneControlData = new RedstoneControlData();
Map.Entry<BlockPos, BlockBreakingProgress> currentBlock;
public boolean sneaking = false;

public BlockBreakerT1BE(BlockEntityType<?> pType, BlockPos pPos, BlockState pBlockState) {
super(pType, pPos, pBlockState);
Expand All @@ -51,6 +53,11 @@ public BlockBreakerT1BE(BlockPos pPos, BlockState pBlockState) {
this(Registration.BlockBreakerT1BE.get(), pPos, pBlockState);
}

public void setBreakerSettings(boolean sneaking) {
this.sneaking = sneaking;
markDirtyClient();
}

@Override
public RedstoneControlData getRedstoneControlData() {
return redstoneControlData;
Expand Down Expand Up @@ -96,16 +103,18 @@ public void clearTrackerIfNeeded(ItemStack tool, FakePlayer fakePlayer) {
@Override
public void tickServer() {
super.tickServer();
doBlockBreak();
FakePlayer fakePlayer = getFakePlayer((ServerLevel) level);
fakePlayer.setShiftKeyDown(sneaking);
doBlockBreak(fakePlayer);
fakePlayer.setShiftKeyDown(false);
}

public boolean canMine() {
return true;
}

public void doBlockBreak() {
public void doBlockBreak(FakePlayer fakePlayer) {
ItemStack tool = getTool();
FakePlayer fakePlayer = getFakePlayer((ServerLevel) level);
clearTrackerIfNeeded(tool, fakePlayer);
if (tool.isEmpty()) {
getRedstoneControlData().pulsed = false;
Expand Down Expand Up @@ -268,6 +277,20 @@ public boolean isDefaultSettings() {
return false;
if (!getRedstoneControlData().equals(getDefaultRedstoneData()))
return false;
if (sneaking)
return false;
return true;
}

@Override
public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
super.saveAdditional(tag, provider);
tag.putBoolean("sneaking", sneaking);
}

@Override
public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) {
this.sneaking = tag.getBoolean("sneaking");
super.loadAdditional(tag, provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static void registerNetworking(final RegisterPayloadHandlersEvent event)
//Going to Server
registrar.playToServer(AreaAffectingPayload.TYPE, AreaAffectingPayload.STREAM_CODEC, AreaAffectingPacket.get()::handle);
registrar.playToServer(BlockStateFilterPayload.TYPE, BlockStateFilterPayload.STREAM_CODEC, BlockStateFilterPacket.get()::handle);
registrar.playToServer(BreakerPayload.TYPE, BreakerPayload.STREAM_CODEC, BreakerPacket.get()::handle);
registrar.playToServer(ClickerPayload.TYPE, ClickerPayload.STREAM_CODEC, ClickerPacket.get()::handle);
registrar.playToServer(CopyMachineSettingsPayload.TYPE, CopyMachineSettingsPayload.STREAM_CODEC, CopyMachineSettingsPacket.get()::handle);
registrar.playToServer(DirectionSettingPayload.TYPE, DirectionSettingPayload.STREAM_CODEC, DirectionSettingPacket.get()::handle);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.direwolf20.justdirethings.common.network.data;

import com.direwolf20.justdirethings.JustDireThings;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;

public record BreakerPayload(
boolean sneaking
) implements CustomPacketPayload {
public static final Type<BreakerPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "breaker_packet"));

@Override
public Type<? extends CustomPacketPayload> type() {
return TYPE;
}

public static final StreamCodec<ByteBuf, BreakerPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.BOOL, BreakerPayload::sneaking,
BreakerPayload::new
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.direwolf20.justdirethings.common.network.handler;

import com.direwolf20.justdirethings.common.blockentities.BlockBreakerT1BE;
import com.direwolf20.justdirethings.common.containers.basecontainers.BaseMachineContainer;
import com.direwolf20.justdirethings.common.network.data.BreakerPayload;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.neoforged.neoforge.network.handling.IPayloadContext;

public class BreakerPacket {
public static final BreakerPacket INSTANCE = new BreakerPacket();

public static BreakerPacket get() {
return INSTANCE;
}

public void handle(final BreakerPayload payload, final IPayloadContext context) {
context.enqueueWork(() -> {
Player sender = context.player();
AbstractContainerMenu container = sender.containerMenu;

if (container instanceof BaseMachineContainer baseMachineContainer && baseMachineContainer.baseMachineBE instanceof BlockBreakerT1BE breaker) {
breaker.setBreakerSettings(payload.sneaking());
}
});
}
}