generated from neoforged/MDK
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I think i'm finally storing/retrieving exp correctly
- Loading branch information
1 parent
8ca3661
commit 95d2c4c
Showing
10 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
src/generated/resources/.cache/8202586f691eec5ad0bb88d13a278951d0c130fb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
// 1.21.1 2024-09-12T11:15:53.905581 Languages: en_us for mod: justdirethings | ||
80317086157715c924dcacf357e72efab49d8580 assets/justdirethings/lang/en_us.json | ||
// 1.21.1 2024-09-12T13:18:00.5916321 Languages: en_us for mod: justdirethings | ||
370a2cb7525636b760559d6e7176176093558760 assets/justdirethings/lang/en_us.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/direwolf20/justdirethings/client/screens/ExperienceHolderScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,44 @@ | ||
package com.direwolf20.justdirethings.client.screens; | ||
|
||
import com.direwolf20.justdirethings.client.screens.basescreens.BaseMachineScreen; | ||
import com.direwolf20.justdirethings.client.screens.standardbuttons.ToggleButtonFactory; | ||
import com.direwolf20.justdirethings.common.blockentities.ExperienceHolderBE; | ||
import com.direwolf20.justdirethings.common.containers.ExperienceHolderContainer; | ||
import com.direwolf20.justdirethings.common.network.data.ExperienceHolderPayload; | ||
import net.minecraft.client.gui.screens.Screen; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.world.entity.player.Inventory; | ||
import net.neoforged.neoforge.network.PacketDistributor; | ||
|
||
public class ExperienceHolderScreen extends BaseMachineScreen<ExperienceHolderContainer> { | ||
private ExperienceHolderBE experienceHolderBE; | ||
private int exp; | ||
public ExperienceHolderScreen(ExperienceHolderContainer container, Inventory inv, Component name) { | ||
super(container, inv, name); | ||
if (container.baseMachineBE instanceof ExperienceHolderBE experienceHolderBE) { | ||
this.experienceHolderBE = experienceHolderBE; | ||
this.exp = experienceHolderBE.exp; | ||
} | ||
} | ||
|
||
@Override | ||
public void init() { | ||
super.init(); | ||
addRenderableWidget(ToggleButtonFactory.STOREEXPBUTTON(getGuiLeft() + 62, topSectionTop + 62, true, b -> { | ||
int amt = 1; | ||
if (Screen.hasControlDown()) | ||
amt = -1; | ||
else if (Screen.hasShiftDown()) | ||
amt = amt * 10; | ||
PacketDistributor.sendToServer(new ExperienceHolderPayload(true, amt)); | ||
})); | ||
addRenderableWidget(ToggleButtonFactory.EXTRACTEXPBUTTON(getGuiLeft() + 102, topSectionTop + 62, true, b -> { | ||
int amt = 1; | ||
if (Screen.hasControlDown()) | ||
amt = -1; | ||
else if (Screen.hasShiftDown()) | ||
amt = amt * 10; | ||
PacketDistributor.sendToServer(new ExperienceHolderPayload(false, amt)); | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/direwolf20/justdirethings/common/network/data/ExperienceHolderPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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 ExperienceHolderPayload( | ||
boolean add, | ||
int levels | ||
) implements CustomPacketPayload { | ||
public static final Type<ExperienceHolderPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(JustDireThings.MODID, "experience_holder")); | ||
|
||
@Override | ||
public Type<ExperienceHolderPayload> type() { | ||
return TYPE; | ||
} | ||
|
||
public static final StreamCodec<FriendlyByteBuf, ExperienceHolderPayload> STREAM_CODEC = StreamCodec.composite( | ||
ByteBufCodecs.BOOL, ExperienceHolderPayload::add, | ||
ByteBufCodecs.INT, ExperienceHolderPayload::levels, | ||
ExperienceHolderPayload::new | ||
); | ||
} |
30 changes: 30 additions & 0 deletions
30
...ain/java/com/direwolf20/justdirethings/common/network/handler/ExperienceHolderPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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.ExperienceHolderPayload; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.inventory.AbstractContainerMenu; | ||
import net.neoforged.neoforge.network.handling.IPayloadContext; | ||
|
||
public class ExperienceHolderPacket { | ||
public static final ExperienceHolderPacket INSTANCE = new ExperienceHolderPacket(); | ||
|
||
public static ExperienceHolderPacket get() { | ||
return INSTANCE; | ||
} | ||
|
||
public void handle(final ExperienceHolderPayload payload, final IPayloadContext context) { | ||
context.enqueueWork(() -> { | ||
Player sender = context.player(); | ||
AbstractContainerMenu container = sender.containerMenu; | ||
|
||
if (container instanceof ExperienceHolderContainer experienceHolderContainer && experienceHolderContainer.baseMachineBE instanceof ExperienceHolderBE experienceHolderBE) { | ||
if (payload.add()) | ||
experienceHolderBE.storeExp(sender, payload.levels()); | ||
else | ||
experienceHolderBE.extractExp(sender, payload.levels()); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/direwolf20/justdirethings/util/ExperienceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.direwolf20.justdirethings.util; | ||
|
||
import net.minecraft.world.entity.player.Player; | ||
|
||
public class ExperienceUtils { | ||
// Calculate experience required to go from level 0 to target level | ||
public static int getTotalExperienceForLevel(int level) { | ||
if (level <= 16) { | ||
return level * level + 6 * level; | ||
} else if (level <= 31) { | ||
return (int) (2.5 * level * level - 40.5 * level + 360); | ||
} else { | ||
return (int) (4.5 * level * level - 162.5 * level + 2220); | ||
} | ||
} | ||
|
||
// Remove levels from a player, ensuring they don't lose more than available | ||
public static int removeLevels(Player player, int levelsToRemove) { | ||
int currentTotalExp = getPlayerTotalExperience(player); | ||
int targetLevel = Math.max(0, player.experienceLevel - levelsToRemove); | ||
|
||
// Calculate how much exp is required to be at the target level | ||
int targetTotalExp = getTotalExperienceForLevel(targetLevel); | ||
int expToRemove = currentTotalExp - targetTotalExp; | ||
|
||
player.giveExperienceLevels(-levelsToRemove); | ||
return expToRemove; // Amount of exp removed | ||
} | ||
|
||
// Calculate experience required to go from one level to the next | ||
public static int getExperienceForNextLevel(int level) { | ||
if (level >= 30) { | ||
return 112 + (level - 30) * 9; | ||
} else { | ||
return level >= 15 ? 37 + (level - 15) * 5 : 7 + level * 2; | ||
} | ||
} | ||
|
||
// Calculate total experience points player currently has (given level and progress) | ||
public static int getPlayerTotalExperience(Player player) { | ||
int exp = getTotalExperienceForLevel(player.experienceLevel); | ||
exp += Math.round(player.experienceProgress * player.getXpNeededForNextLevel()); | ||
return exp; | ||
} | ||
|
||
public static int getExpNeededForNextLevel(Player player) { | ||
return player.getXpNeededForNextLevel() - (int) (player.experienceProgress * player.getXpNeededForNextLevel()); | ||
} | ||
|
||
// Remove points from a player, ensuring they don't lose more than available | ||
public static int removePoints(Player player, int pointsToRemove) { | ||
int currentTotalExp = getPlayerTotalExperience(player); | ||
int expToRemove = Math.min(currentTotalExp, pointsToRemove); | ||
player.giveExperiencePoints(-expToRemove); | ||
return expToRemove; // Amount of exp removed | ||
} | ||
} |