Skip to content

Commit

Permalink
Exp sending automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Sep 13, 2024
1 parent 3e6d6f3 commit 23da09e
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions src/generated/resources/assets/justdirethings/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +18,7 @@
public class ExperienceHolderScreen extends BaseMachineScreen<ExperienceHolderContainer> {
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");

Expand All @@ -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;
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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());

Expand All @@ -144,19 +178,24 @@ private void handleExperience() {
if (entityList.isEmpty()) return;

for (Player player : entityList) {

if (player.experienceLevel != targetExp) {
this.currentPlayer = player;
return;
}
}
}

@Override
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 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<ExperienceHolderSettingsPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "experience_holder_settings"));

@Override
public Type<ExperienceHolderSettingsPayload> type() {
return TYPE;
}

public static final StreamCodec<FriendlyByteBuf, ExperienceHolderSettingsPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, ExperienceHolderSettingsPayload::targetExp,
ExperienceHolderSettingsPayload::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.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());
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 23da09e

Please sign in to comment.