Skip to content

Commit 5ea8b59

Browse files
committed
add drop all function
1 parent e5d2565 commit 5ea8b59

File tree

4 files changed

+368
-320
lines changed

4 files changed

+368
-320
lines changed

src/main/java/com/robertx22/mine_and_slash/capability/player/container/BackpackQuickLootButton.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.client.gui.GuiGraphics;
1111
import net.minecraft.client.gui.components.ImageButton;
1212
import net.minecraft.client.gui.components.Tooltip;
13+
import net.minecraft.client.gui.screens.Screen;
1314
import net.minecraft.client.gui.screens.inventory.ContainerScreen;
1415
import net.minecraft.resources.ResourceLocation;
1516
import net.minecraft.world.inventory.Slot;
@@ -18,10 +19,19 @@
1819
public class BackpackQuickLootButton extends ImageButton {
1920
static ResourceLocation TEXTURE = SlashRef.id("textures/gui/backpack_button.png");
2021
public BackpackQuickLootButton(int pX, int pY, ContainerScreen screen) {
21-
super(pX + ClientConfigs.getConfig().QUICK_LOOT_BUTTON_X_OFFSET.get(), pY + ClientConfigs.getConfig().QUICK_LOOT_BUTTON_Y_OFFSET.get(), 14, 14, 0, 0, 16, TEXTURE, 16, 32, x -> Packets.sendToServer(new BackPackLootMenuPacket()), Gui.MASTER_BACKPACK_LOOT_BUTTON.locName());
22-
this.setTooltip(Tooltip.create(Gui.MASTER_BACKPACK_LOOT_BUTTON.locName()));
22+
super(pX + ClientConfigs.getConfig().QUICK_LOOT_BUTTON_X_OFFSET.get(), pY + ClientConfigs.getConfig().QUICK_LOOT_BUTTON_Y_OFFSET.get(), 14, 14, 0, 0, 16, TEXTURE, 16, 32, x -> Packets.sendToServer(new BackPackLootMenuPacket(Screen.hasShiftDown() ? BackPackLootMenuPacket.Mode.DROP : BackPackLootMenuPacket.Mode.LOOT)), Gui.MASTER_BACKPACK_LOOT_BUTTON.locName());
23+
24+
}
25+
26+
@Override
27+
public void renderWidget(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPartialTick) {
28+
super.renderWidget(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
29+
if (Screen.hasShiftDown()){
30+
this.setTooltip(Tooltip.create(Gui.MASTER_BACKPACK_LOOT_BUTTON_2.locName()));
31+
} else {
32+
this.setTooltip(Tooltip.create(Gui.MASTER_BACKPACK_LOOT_BUTTON.locName()));
33+
}
2334
}
24-
2535

2636
public static void addLootButton(ScreenEvent.Init event){
2737
if (!(event instanceof ScreenEvent.Init.Post)) return;

src/main/java/com/robertx22/mine_and_slash/uncommon/localization/Gui.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public enum Gui implements IAutoLocName {
1010
TALENT_SCREEN_SEARCH_KEYWORD_GAME_CHANGER("game changer"),
1111
TALENT_SCREEN_SEARCH_KEYWORD_ALL("all"),
1212
TALENT_SCREEN_SEARCH_TIPS("Search Keywords: \n\n%1$s: show all allocated talents\n\n%2$s: show all game changer talents"),
13-
MASTER_BACKPACK_LOOT_BUTTON("Click to loot all eligible items to master backpack"),
13+
MASTER_BACKPACK_LOOT_BUTTON("Click to loot all eligible items to master backpack\n\nPress Shift to change mode"),
14+
MASTER_BACKPACK_LOOT_BUTTON_2("Click to drop all items to the ground"),
1415
STATION_LOCK_RECIPE("Lock Recipe"),
1516
STATION_UNLOCK_RECIPE("Unlock Recipe"),
1617
STATION_START_CRAFTING("Start Crafting"),

src/main/java/com/robertx22/mine_and_slash/vanilla_mc/packets/backpack/BackPackLootMenuPacket.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,78 @@
88
import net.minecraft.network.FriendlyByteBuf;
99
import net.minecraft.resources.ResourceLocation;
1010
import net.minecraft.server.level.ServerPlayer;
11+
import net.minecraft.world.entity.item.ItemEntity;
1112
import net.minecraft.world.entity.player.Inventory;
13+
import net.minecraft.world.entity.player.Player;
1214
import net.minecraft.world.inventory.ChestMenu;
1315
import net.minecraft.world.inventory.Slot;
16+
import net.minecraft.world.item.ItemStack;
17+
import net.minecraft.world.item.Items;
18+
19+
import java.util.List;
1420

1521
public class BackPackLootMenuPacket extends MyPacket<BackPackLootMenuPacket> {
22+
Mode mode;
23+
24+
public BackPackLootMenuPacket() {
25+
}
26+
27+
28+
public BackPackLootMenuPacket(Mode mode) {
29+
this.mode = mode;
30+
}
31+
1632
@Override
1733
public ResourceLocation getIdentifier() {
1834
return SlashRef.id("backpack_loot");
1935
}
2036

2137
@Override
2238
public void loadFromData(FriendlyByteBuf friendlyByteBuf) {
23-
39+
mode = friendlyByteBuf.readEnum(Mode.class);
2440
}
2541

2642
@Override
2743
public void saveToData(FriendlyByteBuf friendlyByteBuf) {
28-
44+
friendlyByteBuf.writeEnum(mode);
2945
}
3046

3147
@Override
3248
public void onReceived(ExilePacketContext exilePacketContext) {
3349
if (exilePacketContext.getPlayer() instanceof ServerPlayer player) {
3450
Backpacks backpacks = Load.backpacks(player).getBackpacks();
35-
if (player.hasContainerOpen() && player.containerMenu instanceof ChestMenu chestMenu) {
36-
for (Slot slot : chestMenu.slots) {
37-
if (slot.container instanceof Inventory) continue;
38-
backpacks.tryAutoPickup(player, slot.getItem(), false);
51+
52+
if (player.hasContainerOpen() && player.containerMenu instanceof ChestMenu chestMenu) {
53+
for (Slot slot : chestMenu.slots) {
54+
if (slot.container instanceof Inventory) continue;
55+
ItemStack item = slot.getItem();
56+
if (item.is(Items.AIR)) continue;
57+
if (mode == Mode.LOOT){
58+
backpacks.tryAutoPickup(player, item, false);
59+
} else {
60+
turnItemToPickableAndRemove(item, player);
61+
}
62+
}
3963
}
40-
}
4164

4265
}
4366
}
4467

68+
public static void turnItemToPickableAndRemove(ItemStack itemStacks, Player player){
69+
ItemEntity itemEntity = player.spawnAtLocation(itemStacks.copy(), 1.0f);
70+
if (itemEntity != null){
71+
itemEntity.setNoPickUpDelay();
72+
}
73+
itemStacks.shrink(itemStacks.getCount());
74+
}
75+
4576
@Override
4677
public MyPacket<BackPackLootMenuPacket> newInstance() {
4778
return new BackPackLootMenuPacket();
4879
}
80+
81+
public enum Mode {
82+
LOOT,
83+
DROP
84+
}
4985
}

0 commit comments

Comments
 (0)