9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2026-01-04 15:41:35 +00:00

Improved check

This commit is contained in:
XiaoMoMi
2024-08-05 19:36:56 +08:00
parent 6731d1f668
commit 709c156ff6
3 changed files with 26 additions and 7 deletions

View File

@@ -20,6 +20,7 @@ package net.momirealms.customfishing.api.mechanic.context;
import net.momirealms.customfishing.api.mechanic.competition.CompetitionGoal;
import net.momirealms.customfishing.api.mechanic.loot.LootType;
import org.bukkit.Location;
import org.bukkit.inventory.EquipmentSlot;
import java.util.Objects;
@@ -79,6 +80,7 @@ public class ContextKeys<T> {
public static final ContextKeys<String> RECORD_FORMATTED = of("record_formatted", String.class);
public static final ContextKeys<Integer> CLICKS_LEFT = of("left_clicks", Integer.class);
public static final ContextKeys<Integer> REQUIRED_TIMES = of("clicks", Integer.class);
public static final ContextKeys<EquipmentSlot> SLOT = of("hand", EquipmentSlot.class);
private final String key;
private final Class<T> type;

View File

@@ -46,7 +46,6 @@ import net.momirealms.sparrow.heart.feature.armorstand.FakeArmorStand;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
@@ -403,7 +402,12 @@ public class BukkitActionManager implements ActionManager<Player> {
return context -> {
if (Math.random() > chance) return;
Player player = context.getHolder();
ItemStack itemStack = mainOrOff ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand();
boolean tempHand = mainOrOff;
EquipmentSlot hand = context.arg(ContextKeys.SLOT);
if (hand == EquipmentSlot.OFF_HAND || hand == EquipmentSlot.HAND) {
tempHand = hand == EquipmentSlot.HAND;
}
ItemStack itemStack = tempHand ? player.getInventory().getItemInMainHand() : player.getInventory().getItemInOffHand();
itemStack.setAmount(Math.max(0, itemStack.getAmount() + amount));
};
} else {
@@ -413,12 +417,22 @@ public class BukkitActionManager implements ActionManager<Player> {
});
registerAction("durability", (args, chance) -> {
if (args instanceof Section section) {
EquipmentSlot slot = EquipmentSlot.valueOf(section.getString("slot", "hand").toUpperCase(Locale.ENGLISH));
EquipmentSlot slot = Optional.ofNullable(section.getString("slot"))
.map(hand -> EquipmentSlot.valueOf(hand.toUpperCase(Locale.ENGLISH)))
.orElse(null);
int amount = section.getInt("amount", 1);
return context -> {
if (Math.random() > chance) return;
Player player = context.getHolder();
ItemStack itemStack = player.getInventory().getItem(slot);
EquipmentSlot tempSlot = slot;
EquipmentSlot equipmentSlot = context.arg(ContextKeys.SLOT);
if (equipmentSlot != null) {
tempSlot = equipmentSlot;
}
if (tempSlot == null) {
return;
}
ItemStack itemStack = player.getInventory().getItem(tempSlot);
if (itemStack.getType() == Material.AIR || itemStack.getAmount() == 0)
return;
if (itemStack.getItemMeta() == null)

View File

@@ -74,17 +74,20 @@ public class BukkitEventManager implements EventManager, Listener {
@EventHandler
public void onInteract(PlayerInteractEvent event) {
if (event.getHand() != EquipmentSlot.HAND)
return;
if (event.getAction() != org.bukkit.event.block.Action.RIGHT_CLICK_AIR && event.getAction() != org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK)
return;
ItemStack itemStack = event.getPlayer().getInventory().getItemInMainHand();
EquipmentSlot slot = event.getHand();
if (slot == null) {
return;
}
ItemStack itemStack = event.getPlayer().getInventory().getItem(slot);
if (itemStack.getType() == Material.AIR || itemStack.getAmount() == 0)
return;
String id = this.plugin.getItemManager().getItemID(itemStack);
Context<Player> context = Context.player(event.getPlayer());
Block clicked = event.getClickedBlock();
context.arg(ContextKeys.OTHER_LOCATION, clicked == null ? event.getPlayer().getLocation() : clicked.getLocation());
context.arg(ContextKeys.SLOT, event.getHand());
List<MechanicType> mechanics = MechanicType.getTypeByID(id);
if (mechanics != null) {
for (MechanicType type : mechanics) {