Skip to content

Commit

Permalink
Collect EXP From world
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Sep 13, 2024
1 parent 23da09e commit dd75f7e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21.1 2024-09-13T09:48:33.046202 Languages: en_us for mod: justdirethings
66210d1d8e8869a1f61aa605a2cb56cf6e1f7674 assets/justdirethings/lang/en_us.json
// 1.21.1 2024-09-13T10:08:13.6347516 Languages: en_us for mod: justdirethings
ee74f8b060f11da4663806372cbc2ef7981fadab assets/justdirethings/lang/en_us.json
2 changes: 2 additions & 0 deletions src/generated/resources/assets/justdirethings/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
"justdirethings.screen.click-hold-for": "Hold Click For (ticks)",
"justdirethings.screen.click-left": "Left Click",
"justdirethings.screen.click-right": "Right Click",
"justdirethings.screen.collectexp": "Collect Experience",
"justdirethings.screen.comparecounts": "Compare Stack Sizes",
"justdirethings.screen.comparenbt": "Compare NBT",
"justdirethings.screen.copy_area": "Copy Area",
Expand Down Expand Up @@ -394,6 +395,7 @@
"justdirethings.screen.low": "Low",
"justdirethings.screen.no_fuel": "Fuel source empty",
"justdirethings.screen.notrequireequipped": "Activate from Inventory",
"justdirethings.screen.owneronly": "Owner Only",
"justdirethings.screen.paradoxall": "Revert Blocks and Entities",
"justdirethings.screen.paradoxblock": "Revert Blocks",
"justdirethings.screen.paradoxenergycost": "Energy Cost: %s FE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.NumberButton;
import com.direwolf20.justdirethings.common.blockentities.ExperienceHolderBE;
import com.direwolf20.justdirethings.common.containers.ExperienceHolderContainer;
Expand All @@ -19,6 +20,8 @@ public class ExperienceHolderScreen extends BaseMachineScreen<ExperienceHolderCo
private ExperienceHolderBE experienceHolderBE;
private int exp;
private int targetExp;
private boolean ownerOnly;
private boolean collectExp;
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 @@ -28,6 +31,8 @@ public ExperienceHolderScreen(ExperienceHolderContainer container, Inventory inv
this.experienceHolderBE = experienceHolderBE;
this.exp = experienceHolderBE.exp;
this.targetExp = experienceHolderBE.targetExp;
this.ownerOnly = experienceHolderBE.ownerOnly;
this.collectExp = experienceHolderBE.collectExp;
}
}

Expand All @@ -54,6 +59,16 @@ else if (Screen.hasShiftDown())
targetExp = ((NumberButton) b).getValue(); //The value is updated in the mouseClicked method below
saveSettings();
}));
addRenderableWidget(ToggleButtonFactory.OWNERONLYBUTTON(topSectionLeft + (topSectionWidth / 2) - 15 - 60, topSectionTop + 62, ownerOnly, b -> {
ownerOnly = !ownerOnly;
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
addRenderableWidget(ToggleButtonFactory.COLLECTEXPBUTTON(topSectionLeft + (topSectionWidth / 2) + 15, topSectionTop + 42, collectExp, b -> {
collectExp = !collectExp;
((GrayscaleButton) b).toggleActive();
saveSettings();
}));
}

@Override
Expand Down Expand Up @@ -85,6 +100,6 @@ public void renderXPBar(GuiGraphics guiGraphics, float partialTicks, int mouseX,
@Override
public void saveSettings() {
super.saveSettings();
PacketDistributor.sendToServer(new ExperienceHolderSettingsPayload(targetExp));
PacketDistributor.sendToServer(new ExperienceHolderSettingsPayload(targetExp, ownerOnly, collectExp));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ public static GrayscaleButton STOREEXPBUTTON(int x, int y, boolean startingValue
return new GrayscaleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, STORE_EXP_BUTTON, STORE_EXP_BUTTON_LOCALIZATION, startingValue, onPress);
}

private static final ResourceLocation OWNER_ONLY_BUTTON = ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/player.png");
private static final Component OWNER_ONLY_BUTTON_LOCALIZATION = Component.translatable("justdirethings.screen.owneronly");

public static GrayscaleButton OWNERONLYBUTTON(int x, int y, boolean startingValue, Button.OnPress onPress) {
return new GrayscaleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, OWNER_ONLY_BUTTON, OWNER_ONLY_BUTTON_LOCALIZATION, startingValue, onPress);
}

private static final ResourceLocation COLLECT_EXP_BUTTON = ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/mindfog.png");
private static final Component COLLECT_EXP_BUTTON_LOCALIZATION = Component.translatable("justdirethings.screen.collectexp");

public static GrayscaleButton COLLECTEXPBUTTON(int x, int y, boolean startingValue, Button.OnPress onPress) {
return new GrayscaleButton(x, y, STANDARD_WIDTH, STANDARD_HEIGHT, COLLECT_EXP_BUTTON, COLLECT_EXP_BUTTON_LOCALIZATION, startingValue, onPress);
}

private static final ResourceLocation EXTRACT_EXP_BUTTON = ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "textures/gui/buttons/remove.png");
private static final Component EXTRACT_EXP_BUTTON_LOCALIZATION = Component.translatable("justdirethings.screen.retrieveexp");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.ExperienceOrb;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
Expand All @@ -32,6 +33,8 @@ public class ExperienceHolderBE extends BaseMachineBE implements AreaAffectingBE
public int exp;
public int targetExp;
private Player currentPlayer;
public boolean collectExp;
public boolean ownerOnly;

public ExperienceHolderBE(BlockPos pPos, BlockState pBlockState) {
super(Registration.ExperienceHolderBE.get(), pPos, pBlockState);
Expand All @@ -52,8 +55,10 @@ public AreaAffectingData getAreaAffectingData() {
return areaAffectingData;
}

public void changeSettings(int targetExp) {
public void changeSettings(int targetExp, boolean ownerOnly, boolean collectExp) {
this.targetExp = targetExp;
this.ownerOnly = ownerOnly;
this.collectExp = collectExp;
markDirtyClient();
}

Expand Down Expand Up @@ -128,6 +133,8 @@ public void tickClient() {

public void tickServer() {
super.tickServer();
if (collectExp)
collectExp();
handleExperience();
}

Expand All @@ -136,7 +143,7 @@ public void doParticles(ItemStack itemStack, Vec3 sourcePos, boolean toBlock) {
BlockPos blockPos = getBlockPos();
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() - 0.25f;
double d1 = sourcePos.y();
double d2 = sourcePos.z();
if (toBlock) {
ItemFlowParticleData data = new ItemFlowParticleData(itemStack, baubleSpot.x, baubleSpot.y, baubleSpot.z, 1);
Expand All @@ -158,16 +165,36 @@ private void handleExperience() {
int currentLevel = currentPlayer.experienceLevel;
if (currentLevel < targetExp) {
extractExp(currentPlayer, 1);
doParticles(new ItemStack(Items.EXPERIENCE_BOTTLE), currentPlayer.getEyePosition(), false);
doParticles(new ItemStack(Items.EXPERIENCE_BOTTLE), currentPlayer.getEyePosition().subtract(0, 0.25f, 0), false);
if (exp == 0)
currentPlayer = null; //Clear current target if we run out of exp
} else if (currentLevel > targetExp) {
} else if (currentLevel > targetExp || currentPlayer.experienceProgress > 0.001f) {
storeExp(currentPlayer, 1);
doParticles(new ItemStack(Items.EXPERIENCE_BOTTLE), currentPlayer.getEyePosition(), true);
doParticles(new ItemStack(Items.EXPERIENCE_BOTTLE), currentPlayer.getEyePosition().subtract(0, 0.25f, 0), true);
} else
currentPlayer = null;
}

private void collectExp() {
if (operationTicks != 0)
return; //Run at the tick speed of the machine, but ignore redstone signal (For now at least)
assert level != null;
AABB searchArea = getAABB(getBlockPos());

List<ExperienceOrb> entityList = level.getEntitiesOfClass(ExperienceOrb.class, searchArea, entity -> true)
.stream().toList();

if (entityList.isEmpty()) return;

for (ExperienceOrb experienceOrb : entityList) {
int orbValue = experienceOrb.getValue();
this.exp += orbValue;
doParticles(new ItemStack(Items.EXPERIENCE_BOTTLE), experienceOrb.position(), true);
experienceOrb.discard();
}
markDirtyClient();
}

private void findTargetPlayer() {
assert level != null;
AABB searchArea = getAABB(getBlockPos());
Expand All @@ -178,7 +205,9 @@ private void findTargetPlayer() {
if (entityList.isEmpty()) return;

for (Player player : entityList) {
if (player.experienceLevel != targetExp) {
if (ownerOnly && !player.getUUID().equals(placedByUUID))
continue;
if (player.experienceLevel != targetExp || player.experienceProgress != 0.0f) {
this.currentPlayer = player;
return;
}
Expand All @@ -190,12 +219,16 @@ public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
super.saveAdditional(tag, provider);
tag.putInt("exp", exp);
tag.putInt("targetExp", targetExp);
tag.putBoolean("collectExp", collectExp);
tag.putBoolean("ownerOnly", ownerOnly);
}

@Override
public void loadAdditional(CompoundTag tag, HolderLookup.Provider provider) {
super.loadAdditional(tag, provider);
exp = tag.getInt("exp");
targetExp = tag.getInt("targetExp");
collectExp = tag.getBoolean("collectExp");
ownerOnly = tag.getBoolean("ownerOnly");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import net.minecraft.resources.ResourceLocation;

public record ExperienceHolderSettingsPayload(
int targetExp
int targetExp,
boolean ownerOnly,
boolean collectExp
) implements CustomPacketPayload {
public static final Type<ExperienceHolderSettingsPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "experience_holder_settings"));

Expand All @@ -19,6 +21,8 @@ public Type<ExperienceHolderSettingsPayload> type() {

public static final StreamCodec<FriendlyByteBuf, ExperienceHolderSettingsPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.INT, ExperienceHolderSettingsPayload::targetExp,
ByteBufCodecs.BOOL, ExperienceHolderSettingsPayload::ownerOnly,
ByteBufCodecs.BOOL, ExperienceHolderSettingsPayload::collectExp,
ExperienceHolderSettingsPayload::new
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void handle(final ExperienceHolderSettingsPayload payload, final IPayload
AbstractContainerMenu container = sender.containerMenu;

if (container instanceof ExperienceHolderContainer experienceHolderContainer && experienceHolderContainer.baseMachineBE instanceof ExperienceHolderBE experienceHolderBE) {
experienceHolderBE.changeSettings(payload.targetExp());
experienceHolderBE.changeSettings(payload.targetExp(), payload.ownerOnly(), payload.collectExp());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ protected void addTranslations() {
add("justdirethings.screen.storeexp", "Store Level");
add("justdirethings.screen.retrieveexp", "Retrieve Level");
add("justdirethings.screen.targetexp", "Target Level");
add("justdirethings.screen.owneronly", "Owner Only");
add("justdirethings.screen.collectexp", "Collect Experience");

//Buttons
//add("justdirethings.buttons.save", "Save");
Expand Down

0 comments on commit dd75f7e

Please sign in to comment.