mirror of
https://github.com/songoda/UltimateRepairing.git
synced 2024-11-01 00:10:19 +01:00
fix item disappearing issue
This commit is contained in:
parent
ce4e72ce43
commit
294fd465f0
@ -3,7 +3,6 @@ package com.songoda.ultimaterepairing.anvil;
|
|||||||
import com.songoda.ultimaterepairing.repair.RepairType;
|
import com.songoda.ultimaterepairing.repair.RepairType;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Item;
|
import org.bukkit.entity.Item;
|
||||||
import org.bukkit.inventory.EquipmentSlot;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
public class PlayerAnvilData {
|
public class PlayerAnvilData {
|
||||||
@ -13,7 +12,7 @@ public class PlayerAnvilData {
|
|||||||
private RepairType type;
|
private RepairType type;
|
||||||
private Item item;
|
private Item item;
|
||||||
private ItemStack toBeRepaired;
|
private ItemStack toBeRepaired;
|
||||||
private EquipmentSlot slot = null;
|
private int slot = -1;
|
||||||
private Location locations;
|
private Location locations;
|
||||||
private boolean inRepair;
|
private boolean inRepair;
|
||||||
private boolean beingRepaired;
|
private boolean beingRepaired;
|
||||||
@ -82,11 +81,11 @@ public class PlayerAnvilData {
|
|||||||
this.beingRepaired = beingRepaired;
|
this.beingRepaired = beingRepaired;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EquipmentSlot getSlot() {
|
public int getSlot() {
|
||||||
return slot;
|
return slot;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSlot(EquipmentSlot slot) {
|
public void setSlot(int slot) {
|
||||||
this.slot = slot;
|
this.slot = slot;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -68,10 +68,11 @@ public class RepairGui extends Gui {
|
|||||||
init(type.getNext(player)));
|
init(type.getNext(player)));
|
||||||
|
|
||||||
int i = 9;
|
int i = 9;
|
||||||
for (ItemStack item : player.getInventory().getContents()) {
|
for (int playerslot = 0; playerslot < player.getInventory().getContents().length; playerslot++) {
|
||||||
|
ItemStack item = player.getInventory().getContents()[playerslot];
|
||||||
if (item == null || item.getDurability() <= 0 || item.getMaxStackSize() != 1) continue;
|
if (item == null || item.getDurability() <= 0 || item.getMaxStackSize() != 1) continue;
|
||||||
|
|
||||||
ItemStack toRepair = item;
|
ItemStack toRepair = item.clone();
|
||||||
|
|
||||||
short durability = item.getDurability();
|
short durability = item.getDurability();
|
||||||
|
|
||||||
@ -95,9 +96,10 @@ public class RepairGui extends Gui {
|
|||||||
}
|
}
|
||||||
item.setDurability(durability);
|
item.setDurability(durability);
|
||||||
|
|
||||||
|
int finalplayerslot = playerslot;
|
||||||
setButton(i, item, (event) -> {
|
setButton(i, item, (event) -> {
|
||||||
exit();
|
exit();
|
||||||
UltimateRepairing.getInstance().getRepairHandler().preRepair(toRepair, player, type, anvil);
|
UltimateRepairing.getInstance().getRepairHandler().preRepair(toRepair, finalplayerslot, player, type, anvil);
|
||||||
});
|
});
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ import org.bukkit.*;
|
|||||||
import org.bukkit.entity.Item;
|
import org.bukkit.entity.Item;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.EntityEquipment;
|
import org.bukkit.inventory.EntityEquipment;
|
||||||
import org.bukkit.inventory.EquipmentSlot;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.metadata.FixedMetadataValue;
|
import org.bukkit.metadata.FixedMetadataValue;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
@ -49,35 +48,12 @@ public class RepairHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void preRepair(ItemStack itemStack, Player player, RepairType type, Location anvil) {
|
public void preRepair(ItemStack itemStack, int playerslot, Player player, RepairType type, Location anvil) {
|
||||||
// Get from Map, put new instance in Map if it doesn't exist
|
// Get from Map, put new instance in Map if it doesn't exist
|
||||||
PlayerAnvilData playerData = playerAnvilData.computeIfAbsent(player.getUniqueId(), uuid -> new PlayerAnvilData());
|
PlayerAnvilData playerData = playerAnvilData.computeIfAbsent(player.getUniqueId(), uuid -> new PlayerAnvilData());
|
||||||
|
|
||||||
EntityEquipment equipment = player.getEquipment();
|
player.getInventory().setItem(playerslot, null);
|
||||||
if (equipment != null) {
|
playerData.setSlot(playerslot);
|
||||||
if (equipment.getItemInHand().equals(itemStack)) {
|
|
||||||
equipment.setItemInHand(null);
|
|
||||||
playerData.setSlot(EquipmentSlot.HAND);
|
|
||||||
} else if (itemStack.equals(equipment.getHelmet())) {
|
|
||||||
equipment.setHelmet(null);
|
|
||||||
playerData.setSlot(EquipmentSlot.HEAD);
|
|
||||||
} else if (itemStack.equals(equipment.getChestplate())) {
|
|
||||||
equipment.setChestplate(null);
|
|
||||||
playerData.setSlot(EquipmentSlot.CHEST);
|
|
||||||
} else if (itemStack.equals(equipment.getLeggings())) {
|
|
||||||
equipment.setLeggings(null);
|
|
||||||
playerData.setSlot(EquipmentSlot.LEGS);
|
|
||||||
} else if (itemStack.equals(equipment.getBoots())) {
|
|
||||||
equipment.setBoots(null);
|
|
||||||
playerData.setSlot(EquipmentSlot.FEET);
|
|
||||||
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) {
|
|
||||||
if (itemStack.equals(equipment.getItemInOffHand())) {
|
|
||||||
equipment.setItemInOffHand(null);
|
|
||||||
playerData.setSlot(EquipmentSlot.OFF_HAND);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
player.getInventory().removeItem(itemStack);
|
|
||||||
|
|
||||||
Item item = player.getWorld().dropItem(anvil.add(0.5, 2, 0.5), itemStack);
|
Item item = player.getWorld().dropItem(anvil.add(0.5, 2, 0.5), itemStack);
|
||||||
|
|
||||||
@ -234,31 +210,12 @@ public class RepairHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void removeItem(PlayerAnvilData playerData, Player player) {
|
public void removeItem(PlayerAnvilData playerData, Player player) {
|
||||||
EquipmentSlot slot = playerData.getSlot();
|
int slot = playerData.getSlot();
|
||||||
if (slot == null)
|
if (slot < 0)
|
||||||
PlayerUtils.giveItem(player, playerData.getToBeRepaired());
|
PlayerUtils.giveItem(player, playerData.getToBeRepaired());
|
||||||
else if (player.getEquipment() != null) {
|
else {
|
||||||
EntityEquipment equipment = player.getEquipment();
|
|
||||||
ItemStack item = playerData.getToBeRepaired();
|
ItemStack item = playerData.getToBeRepaired();
|
||||||
switch (slot) {
|
player.getInventory().setItem(slot, item);
|
||||||
case HAND:
|
|
||||||
equipment.setItemInHand(item);
|
|
||||||
break;
|
|
||||||
case OFF_HAND:
|
|
||||||
equipment.setItemInOffHand(item);
|
|
||||||
break;
|
|
||||||
case HEAD:
|
|
||||||
equipment.setHelmet(item);
|
|
||||||
break;
|
|
||||||
case CHEST:
|
|
||||||
equipment.setChestplate(item);
|
|
||||||
break;
|
|
||||||
case LEGS:
|
|
||||||
equipment.setLeggings(item);
|
|
||||||
break;
|
|
||||||
case FEET:
|
|
||||||
equipment.setBoots(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (playerData.getItem() != null)
|
if (playerData.getItem() != null)
|
||||||
playerData.getItem().remove();
|
playerData.getItem().remove();
|
||||||
|
Loading…
Reference in New Issue
Block a user