Skip to content

Commit b0cfb67

Browse files
authored
Merge pull request RobertSkalko#197 from TUsama/quick-drink
add quick consume potion feature
2 parents 08a6d38 + af94018 commit b0cfb67

File tree

8 files changed

+398
-249
lines changed

8 files changed

+398
-249
lines changed

src/main/java/com/robertx22/mine_and_slash/event_hooks/player/OnKeyPress.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.robertx22.mine_and_slash.mmorpg.registers.client.KeybindsRegister;
77
import com.robertx22.mine_and_slash.mmorpg.registers.client.SpellKeybind;
88
import com.robertx22.mine_and_slash.uncommon.utilityclasses.ChatUtils;
9+
import com.robertx22.mine_and_slash.vanilla_mc.packets.QuickUsePotionPacket;
910
import com.robertx22.mine_and_slash.vanilla_mc.packets.UnsummonPacket;
1011
import com.robertx22.mine_and_slash.vanilla_mc.packets.spells.TellServerToCastSpellPacket;
1112
import net.minecraft.client.Minecraft;
@@ -41,6 +42,8 @@ public static void onEndTick(Minecraft mc) {
4142
} else if (KeybindsRegister.HOTBAR_SWAP.isDown()) {
4243
SpellKeybind.IS_ON_SECONd_HOTBAR = !SpellKeybind.IS_ON_SECONd_HOTBAR;
4344
cooldown = 5;
45+
} else if (KeybindsRegister.QUICK_DRINK_POTION.consumeClick()) {
46+
Packets.sendToServer(new QuickUsePotionPacket());
4447
} else {
4548

4649
int number = -1;

src/main/java/com/robertx22/mine_and_slash/mmorpg/registers/client/KeybindsRegister.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package com.robertx22.mine_and_slash.mmorpg.registers.client;
22

3+
import com.mojang.blaze3d.platform.InputConstants;
34
import com.robertx22.mine_and_slash.mmorpg.SlashRef;
45
import net.minecraft.client.KeyMapping;
56
import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
7+
import net.minecraftforge.client.settings.KeyConflictContext;
68
import net.minecraftforge.client.settings.KeyModifier;
79
import org.lwjgl.glfw.GLFW;
810

911
public class KeybindsRegister {
1012

11-
static String CATEGORY = "key." + SlashRef.MODID;
13+
static String CATEGORY = "key." + SlashRef.MODID + ".keybind";
14+
static String prefix = SlashRef.MODID + ".key.";
1215

13-
public static KeyMapping HUB_SCREEN_KEY = new KeyMapping("hub_screen", GLFW.GLFW_KEY_H, CATEGORY);
16+
public static KeyMapping HUB_SCREEN_KEY = new KeyMapping(prefix + "hub_screen", GLFW.GLFW_KEY_H, CATEGORY);
1417

15-
public static KeyMapping UNSUMMON = new KeyMapping("unsummon", GLFW.GLFW_KEY_MINUS, CATEGORY);
18+
public static KeyMapping UNSUMMON = new KeyMapping(prefix + "unsummon", GLFW.GLFW_KEY_MINUS, CATEGORY);
1619

17-
public static KeyMapping HOTBAR_SWAP = new KeyMapping("hotbar_swap", GLFW.GLFW_KEY_F1, CATEGORY);
20+
public static KeyMapping HOTBAR_SWAP = new KeyMapping(prefix + "hotbar_swap", GLFW.GLFW_KEY_F1, CATEGORY);
21+
22+
public static KeyMapping QUICK_DRINK_POTION = new KeyMapping(prefix + "quick_drink_potion", KeyConflictContext.IN_GAME, InputConstants.Type.KEYSYM,GLFW.GLFW_KEY_P, CATEGORY);
1823

1924
public static SpellKeybind SPELL_HOTBAR_1 = new SpellKeybind(1, GLFW.GLFW_KEY_R, null, true);
2025
public static SpellKeybind SPELL_HOTBAR_2 = new SpellKeybind(2, GLFW.GLFW_KEY_V, null, true);
@@ -34,7 +39,7 @@ public static void register(RegisterKeyMappingsEvent x) {
3439
x.register(HUB_SCREEN_KEY);
3540
x.register(UNSUMMON);
3641
x.register(HOTBAR_SWAP);
37-
42+
x.register(QUICK_DRINK_POTION);
3843
for (SpellKeybind k : SpellKeybind.ALL) {
3944
x.register(k.key);
4045
}

src/main/java/com/robertx22/mine_and_slash/mmorpg/registers/client/SpellKeybind.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class SpellKeybind {
1919
boolean firstbar;
2020

2121
public SpellKeybind(int num, int key, KeyModifier mod, boolean firstbar) {
22-
this.key = new KeyMapping("spell_" + num, key, KeybindsRegister.CATEGORY);
22+
this.key = new KeyMapping(KeybindsRegister.prefix + "spell_" + num, key, KeybindsRegister.CATEGORY);
2323
this.firstbar = firstbar;
2424
if (mod != null) {
2525
// todo will this work

src/main/java/com/robertx22/mine_and_slash/mmorpg/registers/common/C2SPacketRegister.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static void register() {
4343
Packets.registerClientToServerPacket(MMORPG.NETWORK, new LockRecipePacket(""), i++);
4444

4545
Packets.registerClientToServerPacket(MMORPG.NETWORK, new BackPackLootMenuPacket(), i++);
46+
Packets.registerClientToServerPacket(MMORPG.NETWORK, new QuickUsePotionPacket(), i++);
4647

4748

4849
// Packets.registerClientToServerPacket(MMORPG.NETWORK, new SetupHotbarPacket(), i++);

src/main/java/com/robertx22/mine_and_slash/vanilla_mc/items/SlashPotionItem.java

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.robertx22.mine_and_slash.vanilla_mc.items;
22

3+
import com.robertx22.library_of_exile.deferred.RegObj;
34
import com.robertx22.library_of_exile.utils.SoundUtils;
45
import com.robertx22.mine_and_slash.database.data.profession.ICreativeTabTiered;
56
import com.robertx22.mine_and_slash.database.data.profession.LeveledItem;
@@ -14,6 +15,7 @@
1415
import com.robertx22.mine_and_slash.gui.texts.textblocks.usableitemblocks.UsageBlock;
1516
import com.robertx22.mine_and_slash.mmorpg.registers.common.items.RarityItems;
1617
import com.robertx22.mine_and_slash.saveclasses.unit.ResourceType;
18+
import com.robertx22.mine_and_slash.saveclasses.unit.ResourcesData;
1719
import com.robertx22.mine_and_slash.uncommon.datasaving.Load;
1820
import com.robertx22.mine_and_slash.uncommon.effectdatas.EventBuilder;
1921
import com.robertx22.mine_and_slash.uncommon.effectdatas.rework.RestoreType;
@@ -51,6 +53,10 @@ public SlashPotionItem(String rar, Type type) {
5153
this.type = type;
5254
}
5355

56+
public Type getType() {
57+
return type;
58+
}
59+
5460
@Override
5561
public String locNameForLangFile() {
5662
return StringUTIL.capitalise(rar) + " " + type.name + " Potion";
@@ -69,7 +75,7 @@ public Item getThis() {
6975
@Override
7076
public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents, TooltipFlag pIsAdvanced) {
7177

72-
int num = (int) this.getHealPercent(pStack);
78+
int num = (int) this.type.getHealPercent(pStack);
7379
pTooltipComponents.clear();
7480
pTooltipComponents.addAll(new ExileTooltips()
7581
.accept(new NameBlock(pStack.getHoverName()))
@@ -86,62 +92,93 @@ public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Compo
8692
public InteractionResultHolder<ItemStack> use(Level pLevel, Player p, InteractionHand pUsedHand) {
8793
ItemStack stack = p.getItemInHand(pUsedHand);
8894

89-
9095
if (!pLevel.isClientSide) {
96+
handlePotionRestore(p, stack);
97+
}
9198

92-
if (type == Type.HP) {
93-
EventBuilder.ofRestore(p, p, ResourceType.health, RestoreType.potion, HealthUtils.getMaxHealth(p) * getHealPercent(stack) / 100F).build().Activate();
94-
EventBuilder.ofRestore(p, p, ResourceType.magic_shield, RestoreType.potion, Load.Unit(p).getUnit().magicShieldData().getValue() * getHealPercent(stack) / 100F).build().Activate();
95-
} else {
96-
EventBuilder.ofRestore(p, p, ResourceType.mana, RestoreType.potion, Load.Unit(p).getUnit().manaData().getValue() * getHealPercent(stack) / 100F).build().Activate();
97-
EventBuilder.ofRestore(p, p, ResourceType.energy, RestoreType.potion, Load.Unit(p).getUnit().energyData().getValue() * getHealPercent(stack) / 100F).build().Activate();
98-
}
99-
99+
return InteractionResultHolder.pass(p.getItemInHand(pUsedHand));
100100

101-
for (SlashPotionItem c : getCooldownItems()) {
102-
p.getCooldowns().addCooldown(c, getCooldownTicks());
103-
}
101+
}
104102

103+
public void handlePotionRestore(Player p, ItemStack stack) {
104+
if (type.restoreResource(p, stack, this)) {
105+
type.getSameTypePotions().forEach(x -> p.getCooldowns().addCooldown(x, getCooldownTicks()));
105106
SoundUtils.playSound(p, SoundEvents.GENERIC_DRINK);
106107
stack.shrink(1);
107108
}
108-
109-
110-
return InteractionResultHolder.pass(p.getItemInHand(pUsedHand));
111-
112109
}
113110

114111
public int getCooldownTicks() {
115112
return 20 * 30;
116113
}
117114

118-
public List<SlashPotionItem> getCooldownItems() {
119-
120-
if (type == Type.HP) {
121-
return RarityItems.HEALTH_POTIONS.values().stream().map(x -> x.get()).collect(Collectors.toList());
122-
} else {
123-
return RarityItems.RESOURCE_POTIONS.values().stream().map(x -> x.get()).collect(Collectors.toList());
124-
125-
}
126-
}
127-
128-
public float getHealPercent(ItemStack stack) {
129-
var r = getRarity();
130-
SkillItemTier tier = LeveledItem.getTier(stack);
131-
return 5 + (0.25F * r.stat_percents.max * tier.statMulti); // todo maybe make separate value
132-
}
133115

134116
public GearRarity getRarity() {
135117
return ExileDB.GearRarities().get(rar);
136118
}
137119

138120
public enum Type {
139-
HP("Health", Items.POTATO),
140-
MANA("Mana", Items.CARROT);
121+
HP("Health", Items.POTATO) {
122+
@Override
123+
public boolean restoreResource(Player player, ItemStack itemStack, SlashPotionItem slashPotionItem) {
124+
float healPercent = this.getHealPercent(itemStack);
125+
ResourcesData resources = Load.Unit(player).getResources();
126+
if (HealthUtils.getCurrentHealth(player) < HealthUtils.getMaxHealth(player) || resources.getMagicShield() < resources.getMax(player, ResourceType.magic_shield)) {
127+
EventBuilder.ofRestore(player, player, ResourceType.health, RestoreType.potion, HealthUtils.getMaxHealth(player) * healPercent / 100F).build().Activate();
128+
EventBuilder.ofRestore(player, player, ResourceType.magic_shield, RestoreType.potion, Load.Unit(player).getUnit().magicShieldData().getValue() * healPercent / 100F).build().Activate();
129+
return true;
130+
}
131+
return false;
132+
133+
}
134+
135+
@Override
136+
public List<SlashPotionItem> getSameTypePotions() {
137+
return RarityItems.HEALTH_POTIONS.values().stream().map(RegObj::get).collect(Collectors.toList());
138+
}
139+
140+
@Override
141+
public float getHealPercent(ItemStack stack) {
142+
var r = ((SlashPotionItem) stack.getItem()).getRarity();
143+
SkillItemTier tier = LeveledItem.getTier(stack);
144+
return 5 + (0.25F * r.stat_percents.max * tier.statMulti);
145+
}
146+
},
147+
MANA("Mana", Items.CARROT) {
148+
@Override
149+
public boolean restoreResource(Player player, ItemStack itemStack, SlashPotionItem slashPotionItem) {
150+
float healPercent = this.getHealPercent(itemStack);
151+
ResourcesData resources = Load.Unit(player).getResources();
152+
if (resources.getMana() < resources.getMax(player, ResourceType.mana) || resources.getEnergy() < resources.getMax(player, ResourceType.energy)) {
153+
EventBuilder.ofRestore(player, player, ResourceType.mana, RestoreType.potion, Load.Unit(player).getUnit().manaData().getValue() * healPercent / 100F).build().Activate();
154+
EventBuilder.ofRestore(player, player, ResourceType.energy, RestoreType.potion, Load.Unit(player).getUnit().energyData().getValue() * healPercent / 100F).build().Activate();
155+
156+
return true;
157+
}
158+
return false;
159+
}
160+
161+
@Override
162+
public List<SlashPotionItem> getSameTypePotions() {
163+
return RarityItems.RESOURCE_POTIONS.values().stream().map(RegObj::get).collect(Collectors.toList());
164+
}
165+
166+
@Override
167+
public float getHealPercent(ItemStack stack) {
168+
var r = ((SlashPotionItem) stack.getItem()).getRarity();
169+
SkillItemTier tier = LeveledItem.getTier(stack);
170+
return 5 + (0.25F * r.stat_percents.max * tier.statMulti);
171+
}
172+
};
141173
String name;
142174

143175
Item craftItem;
144176

177+
178+
public abstract boolean restoreResource(Player player, ItemStack itemStack, SlashPotionItem slashPotionItem);
179+
public abstract List<SlashPotionItem> getSameTypePotions();
180+
public abstract float getHealPercent(ItemStack stack);
181+
145182
Type(String name, Item craftItem) {
146183
this.name = name;
147184
this.craftItem = craftItem;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.robertx22.mine_and_slash.vanilla_mc.packets;
2+
3+
import com.robertx22.library_of_exile.main.MyPacket;
4+
import com.robertx22.library_of_exile.packets.ExilePacketContext;
5+
import com.robertx22.mine_and_slash.mmorpg.SlashRef;
6+
import com.robertx22.mine_and_slash.vanilla_mc.items.SlashPotionItem;
7+
import net.minecraft.network.FriendlyByteBuf;
8+
import net.minecraft.resources.ResourceLocation;
9+
import net.minecraft.server.level.ServerPlayer;
10+
import net.minecraft.world.entity.player.Player;
11+
import net.minecraft.world.inventory.Slot;
12+
import net.minecraft.world.item.ItemStack;
13+
import org.apache.commons.lang3.tuple.Pair;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.stream.Collectors;
18+
import java.util.stream.Stream;
19+
20+
public class QuickUsePotionPacket extends MyPacket<QuickUsePotionPacket> {
21+
@Override
22+
public ResourceLocation getIdentifier() {
23+
return SlashRef.id("quick_use_potion");
24+
}
25+
26+
@Override
27+
public void loadFromData(FriendlyByteBuf friendlyByteBuf) {
28+
29+
}
30+
31+
@Override
32+
public void saveToData(FriendlyByteBuf friendlyByteBuf) {
33+
34+
}
35+
36+
@Override
37+
public void onReceived(ExilePacketContext exilePacketContext) {
38+
if (exilePacketContext.getPlayer() instanceof ServerPlayer player) {
39+
List<ItemStack> potionItems = new ArrayList<>();
40+
//get all kinds of SlashPotionItem in player's inventory.
41+
for (Slot slot : player.inventoryMenu.slots) {
42+
if (slot.getItem().getItem() instanceof SlashPotionItem) {
43+
potionItems.add(slot.getItem());
44+
}
45+
}
46+
potionItems.stream()
47+
//remove all in-cooldown potions
48+
.filter(x -> player.getCooldowns().isOnCooldown(x.getItem()))
49+
.map(x -> Pair.of(x, ((SlashPotionItem) x.getItem())))
50+
.collect(Collectors.groupingBy(x -> x.getRight().getType()))
51+
.values()
52+
.stream()
53+
//pick the greatest one
54+
.map(v -> {
55+
if (v.size() > 1) {
56+
//sort the list to find the greatest one
57+
List<Pair<ItemStack, SlashPotionItem>> sorted = v.stream()
58+
.sorted(((p1, p2) -> -Float.compare(p1.getRight().getType().getHealPercent(p1.getKey()), p2.getRight().getType().getHealPercent(p2.getKey()))))
59+
.toList();
60+
return sorted.get(0);
61+
} else {
62+
return v.get(0);
63+
}
64+
65+
})
66+
//try drink
67+
.forEach(x -> x.getRight().handlePotionRestore(player, x.getLeft()));
68+
}
69+
}
70+
71+
@Override
72+
public MyPacket<QuickUsePotionPacket> newInstance() {
73+
return new QuickUsePotionPacket();
74+
}
75+
}

0 commit comments

Comments
 (0)