diff --git a/src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb b/src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb index c1938827..2d9ca116 100644 --- a/src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb +++ b/src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb @@ -1,2 +1,2 @@ -// 1.21.1 2024-09-12T13:18:00.5916321 Languages: en_us for mod: justdirethings -370a2cb7525636b760559d6e7176176093558760 assets/justdirethings/lang/en_us.json +// 1.21.1 2024-09-13T09:48:33.046202 Languages: en_us for mod: justdirethings +66210d1d8e8869a1f61aa605a2cb56cf6e1f7674 assets/justdirethings/lang/en_us.json diff --git a/src/generated/resources/assets/justdirethings/lang/en_us.json b/src/generated/resources/assets/justdirethings/lang/en_us.json index 32b32bf5..42b5d985 100644 --- a/src/generated/resources/assets/justdirethings/lang/en_us.json +++ b/src/generated/resources/assets/justdirethings/lang/en_us.json @@ -408,7 +408,7 @@ "justdirethings.screen.renderarea": "Render Area", "justdirethings.screen.renderparadox": "Render Paradox", "justdirethings.screen.requireequipped": "Activate if Equipped", - "justdirethings.screen.retrieveexp": "Retrieve Exp", + "justdirethings.screen.retrieveexp": "Retrieve Level", "justdirethings.screen.rightclicksettings": "Right Click for Settings", "justdirethings.screen.save_close": "Save and Close", "justdirethings.screen.senditems": "Push Items", @@ -420,7 +420,7 @@ "justdirethings.screen.snapshotarea": "Snapshot Area", "justdirethings.screen.sneak-click": "Sneak Click", "justdirethings.screen.stay_open": "Stay Open", - "justdirethings.screen.storeexp": "Store Exp", + "justdirethings.screen.storeexp": "Store Level", "justdirethings.screen.swapitems": "Swap Items", "justdirethings.screen.target-adult": "Target Adult", "justdirethings.screen.target-air": "Target Air", @@ -432,6 +432,7 @@ "justdirethings.screen.target-noblock": "Ignore Blocks", "justdirethings.screen.target-passive": "Target Passive", "justdirethings.screen.target-player": "Target Player", + "justdirethings.screen.targetexp": "Target Level", "justdirethings.screen.tickspeed": "Speed (Ticks)", "justdirethings.settingscopied": "Settings Copied", "justdirethings.settingspasted": "Settings Pasted", diff --git a/src/main/java/com/direwolf20/justdirethings/client/screens/ExperienceHolderScreen.java b/src/main/java/com/direwolf20/justdirethings/client/screens/ExperienceHolderScreen.java index 45b2a5d1..b904414e 100644 --- a/src/main/java/com/direwolf20/justdirethings/client/screens/ExperienceHolderScreen.java +++ b/src/main/java/com/direwolf20/justdirethings/client/screens/ExperienceHolderScreen.java @@ -2,9 +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.NumberButton; import com.direwolf20.justdirethings.common.blockentities.ExperienceHolderBE; import com.direwolf20.justdirethings.common.containers.ExperienceHolderContainer; import com.direwolf20.justdirethings.common.network.data.ExperienceHolderPayload; +import com.direwolf20.justdirethings.common.network.data.ExperienceHolderSettingsPayload; import com.direwolf20.justdirethings.util.ExperienceUtils; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.screens.Screen; @@ -16,6 +18,7 @@ public class ExperienceHolderScreen extends BaseMachineScreen { private ExperienceHolderBE experienceHolderBE; private int exp; + private int targetExp; private static final ResourceLocation EXPERIENCE_BAR_BACKGROUND_SPRITE = ResourceLocation.withDefaultNamespace("hud/experience_bar_background"); private static final ResourceLocation EXPERIENCE_BAR_PROGRESS_SPRITE = ResourceLocation.withDefaultNamespace("hud/experience_bar_progress"); @@ -24,6 +27,7 @@ public ExperienceHolderScreen(ExperienceHolderContainer container, Inventory inv if (container.baseMachineBE instanceof ExperienceHolderBE experienceHolderBE) { this.experienceHolderBE = experienceHolderBE; this.exp = experienceHolderBE.exp; + this.targetExp = experienceHolderBE.targetExp; } } @@ -46,6 +50,10 @@ else if (Screen.hasShiftDown()) amt = amt * 10; PacketDistributor.sendToServer(new ExperienceHolderPayload(false, amt)); })); + addRenderableWidget(ToggleButtonFactory.TARGETEXPBUTTON(topSectionLeft + (topSectionWidth / 2) - 15 - 42, topSectionTop + 64, targetExp, b -> { + targetExp = ((NumberButton) b).getValue(); //The value is updated in the mouseClicked method below + saveSettings(); + })); } @Override @@ -73,4 +81,10 @@ public void renderXPBar(GuiGraphics guiGraphics, float partialTicks, int mouseX, guiGraphics.drawString(font, s, j, k - 1, 0, false); guiGraphics.drawString(font, s, j, k, 8453920, false); } + + @Override + public void saveSettings() { + super.saveSettings(); + PacketDistributor.sendToServer(new ExperienceHolderSettingsPayload(targetExp)); + } } diff --git a/src/main/java/com/direwolf20/justdirethings/client/screens/standardbuttons/ToggleButtonFactory.java b/src/main/java/com/direwolf20/justdirethings/client/screens/standardbuttons/ToggleButtonFactory.java index 8f91fc0d..4fc69cc0 100644 --- a/src/main/java/com/direwolf20/justdirethings/client/screens/standardbuttons/ToggleButtonFactory.java +++ b/src/main/java/com/direwolf20/justdirethings/client/screens/standardbuttons/ToggleButtonFactory.java @@ -159,6 +159,12 @@ public static NumberButton TICKSPEEDBUTTON(int x, int y, int value, int min, But return new NumberButton(x, y, 24, 12, value, min, 1200, ticksButtonLocalization, onPress); } + private static Component targetExpLocalization = Component.translatable("justdirethings.screen.targetexp"); + + public static NumberButton TARGETEXPBUTTON(int x, int y, int value, Button.OnPress onPress) { + return new NumberButton(x, y, 24, 12, value, 0, 1000, targetExpLocalization, onPress); + } + private static Component pickupDelayButtonLocalization = Component.translatable("justdirethings.screen.pickupdelay"); public static NumberButton PICKUPDELAYBUTTON(int x, int y, int value, Button.OnPress onPress) { diff --git a/src/main/java/com/direwolf20/justdirethings/common/blockentities/ExperienceHolderBE.java b/src/main/java/com/direwolf20/justdirethings/common/blockentities/ExperienceHolderBE.java index d9af263b..ed83ae44 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/blockentities/ExperienceHolderBE.java +++ b/src/main/java/com/direwolf20/justdirethings/common/blockentities/ExperienceHolderBE.java @@ -16,6 +16,7 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.properties.BlockStateProperties; @@ -29,6 +30,8 @@ public class ExperienceHolderBE extends BaseMachineBE implements AreaAffectingBE public AreaAffectingData areaAffectingData = new AreaAffectingData(); public RedstoneControlData redstoneControlData = new RedstoneControlData(); public int exp; + public int targetExp; + private Player currentPlayer; public ExperienceHolderBE(BlockPos pPos, BlockState pBlockState) { super(Registration.ExperienceHolderBE.get(), pPos, pBlockState); @@ -49,6 +52,11 @@ public AreaAffectingData getAreaAffectingData() { return areaAffectingData; } + public void changeSettings(int targetExp) { + this.targetExp = targetExp; + markDirtyClient(); + } + public void storeExp(Player player, int levelChange) { if (levelChange == -1) { // Move all experience from player @@ -123,18 +131,44 @@ public void tickServer() { handleExperience(); } - public void doParticles(ItemStack itemStack, Vec3 sourcePos) { + public void doParticles(ItemStack itemStack, Vec3 sourcePos, boolean toBlock) { Direction direction = getBlockState().getValue(BlockStateProperties.FACING); BlockPos blockPos = getBlockPos(); - ItemFlowParticleData data = new ItemFlowParticleData(itemStack, blockPos.getX() + 0.5f - (0.3 * direction.getStepX()), blockPos.getY() + 0.5f - (0.3 * direction.getStepY()), blockPos.getZ() + 0.5f - (0.3 * direction.getStepZ()), 5); + Vec3 baubleSpot = new Vec3(blockPos.getX() + 0.5f - (0.3 * direction.getStepX()), blockPos.getY() + 0.5f - (0.3 * direction.getStepY()), blockPos.getZ() + 0.5f - (0.3 * direction.getStepZ())); double d0 = sourcePos.x(); - double d1 = sourcePos.y(); + double d1 = sourcePos.y() - 0.25f; double d2 = sourcePos.z(); - ((ServerLevel) level).sendParticles(data, d0, d1, d2, 10, 0, 0, 0, 0); + if (toBlock) { + ItemFlowParticleData data = new ItemFlowParticleData(itemStack, baubleSpot.x, baubleSpot.y, baubleSpot.z, 1); + ((ServerLevel) level).sendParticles(data, d0, d1, d2, 10, 0, 0, 0, 0); + } else { + ItemFlowParticleData data = new ItemFlowParticleData(itemStack, d0, d1, d2, 1); + ((ServerLevel) level).sendParticles(data, baubleSpot.x, baubleSpot.y, baubleSpot.z, 10, 0, 0, 0, 0); + } } private void handleExperience() { - if (!isActiveRedstone() || !canRun()) return; + //if (!isActiveRedstone() || !canRun()) return; + assert level != null; + if (isActiveRedstone() && canRun() && currentPlayer == null) + findTargetPlayer(); + + if (currentPlayer == null) return; + + int currentLevel = currentPlayer.experienceLevel; + if (currentLevel < targetExp) { + extractExp(currentPlayer, 1); + doParticles(new ItemStack(Items.EXPERIENCE_BOTTLE), currentPlayer.getEyePosition(), false); + if (exp == 0) + currentPlayer = null; //Clear current target if we run out of exp + } else if (currentLevel > targetExp) { + storeExp(currentPlayer, 1); + doParticles(new ItemStack(Items.EXPERIENCE_BOTTLE), currentPlayer.getEyePosition(), true); + } else + currentPlayer = null; + } + + private void findTargetPlayer() { assert level != null; AABB searchArea = getAABB(getBlockPos()); @@ -144,7 +178,10 @@ private void handleExperience() { if (entityList.isEmpty()) return; for (Player player : entityList) { - + if (player.experienceLevel != targetExp) { + this.currentPlayer = player; + return; + } } } @@ -152,11 +189,13 @@ private void handleExperience() { public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) { super.saveAdditional(tag, provider); tag.putInt("exp", exp); + tag.putInt("targetExp", targetExp); } @Override public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) { super.loadAdditional(tag, provider); exp = tag.getInt("exp"); + targetExp = tag.getInt("targetExp"); } } diff --git a/src/main/java/com/direwolf20/justdirethings/common/network/PacketHandler.java b/src/main/java/com/direwolf20/justdirethings/common/network/PacketHandler.java index 11333759..076125d0 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/network/PacketHandler.java +++ b/src/main/java/com/direwolf20/justdirethings/common/network/PacketHandler.java @@ -39,6 +39,7 @@ public static void registerNetworking(final RegisterPayloadHandlersEvent event) registrar.playToServer(InventoryHolderSettingsPayload.TYPE, InventoryHolderSettingsPayload.STREAM_CODEC, InventoryHolderSettingsPacket.get()::handle); registrar.playToServer(InventoryHolderMoveItemsPayload.TYPE, InventoryHolderMoveItemsPayload.STREAM_CODEC, InventoryHolderMoveItemsPacket.get()::handle); registrar.playToServer(ExperienceHolderPayload.TYPE, ExperienceHolderPayload.STREAM_CODEC, ExperienceHolderPacket.get()::handle); + registrar.playToServer(ExperienceHolderSettingsPayload.TYPE, ExperienceHolderSettingsPayload.STREAM_CODEC, ExperienceHolderSettingsPacket.get()::handle); //Going to Client registrar.playToClient(ClientSoundPayload.TYPE, ClientSoundPayload.STREAM_CODEC, ClientSoundPacket.get()::handle); diff --git a/src/main/java/com/direwolf20/justdirethings/common/network/data/ExperienceHolderSettingsPayload.java b/src/main/java/com/direwolf20/justdirethings/common/network/data/ExperienceHolderSettingsPayload.java new file mode 100644 index 00000000..207e92c6 --- /dev/null +++ b/src/main/java/com/direwolf20/justdirethings/common/network/data/ExperienceHolderSettingsPayload.java @@ -0,0 +1,24 @@ +package com.direwolf20.justdirethings.common.network.data; + +import com.direwolf20.justdirethings.JustDireThings; +import net.minecraft.network.FriendlyByteBuf; +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 ExperienceHolderSettingsPayload( + int targetExp +) implements CustomPacketPayload { + public static final Type TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "experience_holder_settings")); + + @Override + public Type type() { + return TYPE; + } + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.INT, ExperienceHolderSettingsPayload::targetExp, + ExperienceHolderSettingsPayload::new + ); +} diff --git a/src/main/java/com/direwolf20/justdirethings/common/network/handler/ExperienceHolderSettingsPacket.java b/src/main/java/com/direwolf20/justdirethings/common/network/handler/ExperienceHolderSettingsPacket.java new file mode 100644 index 00000000..3945a2de --- /dev/null +++ b/src/main/java/com/direwolf20/justdirethings/common/network/handler/ExperienceHolderSettingsPacket.java @@ -0,0 +1,27 @@ +package com.direwolf20.justdirethings.common.network.handler; + +import com.direwolf20.justdirethings.common.blockentities.ExperienceHolderBE; +import com.direwolf20.justdirethings.common.containers.ExperienceHolderContainer; +import com.direwolf20.justdirethings.common.network.data.ExperienceHolderSettingsPayload; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.neoforged.neoforge.network.handling.IPayloadContext; + +public class ExperienceHolderSettingsPacket { + public static final ExperienceHolderSettingsPacket INSTANCE = new ExperienceHolderSettingsPacket(); + + public static ExperienceHolderSettingsPacket get() { + return INSTANCE; + } + + public void handle(final ExperienceHolderSettingsPayload payload, final IPayloadContext context) { + context.enqueueWork(() -> { + Player sender = context.player(); + AbstractContainerMenu container = sender.containerMenu; + + if (container instanceof ExperienceHolderContainer experienceHolderContainer && experienceHolderContainer.baseMachineBE instanceof ExperienceHolderBE experienceHolderBE) { + experienceHolderBE.changeSettings(payload.targetExp()); + } + }); + } +} diff --git a/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLanguageProvider.java b/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLanguageProvider.java index 94f01083..732b681b 100644 --- a/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLanguageProvider.java +++ b/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLanguageProvider.java @@ -521,8 +521,9 @@ protected void addTranslations() { add("justdirethings.screen.senditems", "Push Items"); add("justdirethings.screen.pullitems", "Pull Items"); add("justdirethings.screen.swapitems", "Swap Items"); - add("justdirethings.screen.storeexp", "Store Exp"); - add("justdirethings.screen.retrieveexp", "Retrieve Exp"); + add("justdirethings.screen.storeexp", "Store Level"); + add("justdirethings.screen.retrieveexp", "Retrieve Level"); + add("justdirethings.screen.targetexp", "Target Level"); //Buttons //add("justdirethings.buttons.save", "Save");