Documented and improved the codestyle of Proxies and Proxy Implementation

This commit is contained in:
Auxilor 2020-12-23 23:06:46 +00:00
parent 311251686d
commit 7a36872ab3
39 changed files with 230 additions and 110 deletions

View File

@ -5,10 +5,12 @@ import net.minecraft.server.v1_15_R1.BlockPosition;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class BlockBreak implements BlockBreakProxy {
public final class BlockBreak implements BlockBreakProxy {
@Override
public void breakBlock(Player player, Block block) {
public void breakBlock(@NotNull final Player player,
@NotNull final Block block) {
((CraftPlayer) player).getHandle().playerInteractManager.breakBlock(new BlockPosition(block.getX(), block.getY(), block.getZ()));
}
}

View File

@ -1,10 +1,11 @@
package com.willfp.eco.core.proxy.v1_15_R1;
import com.willfp.eco.core.proxy.proxies.ChatComponentProxy;
import org.jetbrains.annotations.NotNull;
public class ChatComponent implements ChatComponentProxy {
public final class ChatComponent implements ChatComponentProxy {
@Override
public Object modifyComponent(Object object) {
public Object modifyComponent(@NotNull final Object object) {
return object;
}
}

View File

@ -4,10 +4,11 @@ import com.willfp.eco.core.proxy.proxies.CooldownProxy;
import net.minecraft.server.v1_15_R1.EntityHuman;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class Cooldown implements CooldownProxy {
public final class Cooldown implements CooldownProxy {
@Override
public double getAttackCooldown(Player player) {
public double getAttackCooldown(@NotNull final Player player) {
EntityHuman entityHuman = ((CraftPlayer) player).getHandle();
return entityHuman.s(0);
}

View File

@ -8,13 +8,14 @@ import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_15_R1.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public class FastGetEnchants implements FastGetEnchantsProxy {
public final class FastGetEnchants implements FastGetEnchantsProxy {
@Override
public Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack) {
public Map<Enchantment, Integer> getEnchantmentsOnItem(@NotNull final ItemStack itemStack) {
net.minecraft.server.v1_15_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
HashMap<Enchantment, Integer> foundEnchantments = new HashMap<>();
@ -31,7 +32,8 @@ public class FastGetEnchants implements FastGetEnchantsProxy {
}
@Override
public int getLevelOnItem(ItemStack itemStack, Enchantment enchantment) {
public int getLevelOnItem(@NotNull final ItemStack itemStack,
@NotNull final Enchantment enchantment) {
net.minecraft.server.v1_15_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();

View File

@ -3,10 +3,11 @@ package com.willfp.eco.core.proxy.v1_15_R1;
import com.willfp.eco.core.proxy.proxies.OpenInventoryProxy;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class OpenInventory implements OpenInventoryProxy {
public final class OpenInventory implements OpenInventoryProxy {
@Override
public Object getOpenInventory(Player player) {
public Object getOpenInventory(@NotNull final Player player) {
return ((CraftPlayer) player).getHandle().activeContainer;
}
}

View File

@ -3,17 +3,19 @@ package com.willfp.eco.core.proxy.v1_15_R1;
import com.willfp.eco.core.proxy.proxies.RepairCostProxy;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class RepairCost implements RepairCostProxy {
public final class RepairCost implements RepairCostProxy {
@Override
public ItemStack setRepairCost(ItemStack itemStack, int cost) {
public ItemStack setRepairCost(@NotNull final ItemStack itemStack,
final int cost) {
net.minecraft.server.v1_15_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
nmsStack.setRepairCost(cost);
return CraftItemStack.asBukkitCopy(nmsStack);
}
@Override
public int getRepairCost(ItemStack itemStack) {
public int getRepairCost(@NotNull final ItemStack itemStack) {
net.minecraft.server.v1_15_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
return nmsStack.getRepairCost();
}

View File

@ -6,10 +6,11 @@ import org.bukkit.craftbukkit.v1_15_R1.entity.CraftTrident;
import org.bukkit.craftbukkit.v1_15_R1.inventory.CraftItemStack;
import org.bukkit.entity.Trident;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class TridentStack implements TridentStackProxy {
public final class TridentStack implements TridentStackProxy {
@Override
public ItemStack getTridentStack(Trident trident) {
public ItemStack getTridentStack(@NotNull final Trident trident) {
EntityThrownTrident t = ((CraftTrident) trident).getHandle();
return CraftItemStack.asBukkitCopy(t.trident);
}

View File

@ -5,10 +5,11 @@ import net.minecraft.server.v1_16_R1.BlockPosition;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class BlockBreak implements BlockBreakProxy {
public final class BlockBreak implements BlockBreakProxy {
@Override
public void breakBlock(Player player, Block block) {
public final void breakBlock(@NotNull final Player player, @NotNull final Block block) {
((CraftPlayer) player).getHandle().playerInteractManager.breakBlock(new BlockPosition(block.getX(), block.getY(), block.getZ()));
}
}

View File

@ -15,12 +15,13 @@ import net.minecraft.server.v1_16_R1.MojangsonParser;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class ChatComponent implements ChatComponentProxy {
public final class ChatComponent implements ChatComponentProxy {
@Override
public Object modifyComponent(Object object) {
public Object modifyComponent(@NotNull final Object object) {
if (!(object instanceof IChatBaseComponent)) {
return object;
}
@ -31,7 +32,7 @@ public class ChatComponent implements ChatComponentProxy {
return chatComponent;
}
public void modifyBaseComponent(IChatBaseComponent component) {
private void modifyBaseComponent(@NotNull final IChatBaseComponent component) {
component.getSiblings().forEach(this::modifyBaseComponent);
if (component instanceof ChatMessage) {
Arrays.stream(((ChatMessage) component).getArgs())
@ -68,7 +69,7 @@ public class ChatComponent implements ChatComponentProxy {
((ChatBaseComponent) component).setChatModifier(modifier);
}
private static ItemStack getFromTag(String jsonTag, String id) {
private static ItemStack getFromTag(@NotNull String jsonTag, @NotNull String id) {
id = id.replace("minecraft:", "");
id = id.toUpperCase();
id = id.replace("\"", "");
@ -89,7 +90,7 @@ public class ChatComponent implements ChatComponentProxy {
return CraftItemStack.asBukkitCopy(nmsStack);
}
private static String toJson(ItemStack itemStack) {
private static String toJson(@NotNull final ItemStack itemStack) {
return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString();
}
}

View File

@ -2,10 +2,11 @@ package com.willfp.eco.core.proxy.v1_16_R1;
import com.willfp.eco.core.proxy.proxies.CooldownProxy;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class Cooldown implements CooldownProxy {
public final class Cooldown implements CooldownProxy {
@Override
public double getAttackCooldown(Player player) {
public double getAttackCooldown(@NotNull final Player player) {
return player.getAttackCooldown();
}
}

View File

@ -8,13 +8,14 @@ import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R1.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public class FastGetEnchants implements FastGetEnchantsProxy {
public final class FastGetEnchants implements FastGetEnchantsProxy {
@Override
public Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack) {
public Map<Enchantment, Integer> getEnchantmentsOnItem(@NotNull final ItemStack itemStack) {
net.minecraft.server.v1_16_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
HashMap<Enchantment, Integer> foundEnchantments = new HashMap<>();
@ -31,7 +32,8 @@ public class FastGetEnchants implements FastGetEnchantsProxy {
}
@Override
public int getLevelOnItem(ItemStack itemStack, Enchantment enchantment) {
public int getLevelOnItem(@NotNull final ItemStack itemStack,
@NotNull final Enchantment enchantment) {
net.minecraft.server.v1_16_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();

View File

@ -3,10 +3,11 @@ package com.willfp.eco.core.proxy.v1_16_R1;
import com.willfp.eco.core.proxy.proxies.OpenInventoryProxy;
import org.bukkit.craftbukkit.v1_16_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class OpenInventory implements OpenInventoryProxy {
public final class OpenInventory implements OpenInventoryProxy {
@Override
public Object getOpenInventory(Player player) {
public Object getOpenInventory(@NotNull final Player player) {
return ((CraftPlayer) player).getHandle().activeContainer;
}
}

View File

@ -3,17 +3,19 @@ package com.willfp.eco.core.proxy.v1_16_R1;
import com.willfp.eco.core.proxy.proxies.RepairCostProxy;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class RepairCost implements RepairCostProxy {
public final class RepairCost implements RepairCostProxy {
@Override
public ItemStack setRepairCost(ItemStack itemStack, int cost) {
public ItemStack setRepairCost(@NotNull final ItemStack itemStack,
final int cost) {
net.minecraft.server.v1_16_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
nmsStack.setRepairCost(cost);
return CraftItemStack.asBukkitCopy(nmsStack);
}
@Override
public int getRepairCost(ItemStack itemStack) {
public int getRepairCost(@NotNull final ItemStack itemStack) {
net.minecraft.server.v1_16_R1.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
return nmsStack.getRepairCost();
}

View File

@ -6,10 +6,11 @@ import org.bukkit.craftbukkit.v1_16_R1.entity.CraftTrident;
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemStack;
import org.bukkit.entity.Trident;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class TridentStack implements TridentStackProxy {
public final class TridentStack implements TridentStackProxy {
@Override
public ItemStack getTridentStack(Trident trident) {
public ItemStack getTridentStack(@NotNull final Trident trident) {
EntityThrownTrident t = ((CraftTrident) trident).getHandle();
return CraftItemStack.asBukkitCopy(t.trident);
}

View File

@ -5,10 +5,12 @@ import net.minecraft.server.v1_16_R2.BlockPosition;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class BlockBreak implements BlockBreakProxy {
public final class BlockBreak implements BlockBreakProxy {
@Override
public void breakBlock(Player player, Block block) {
public void breakBlock(@NotNull final Player player,
@NotNull final Block block) {
((CraftPlayer) player).getHandle().playerInteractManager.breakBlock(new BlockPosition(block.getX(), block.getY(), block.getZ()));
}
}

View File

@ -15,12 +15,13 @@ import net.minecraft.server.v1_16_R2.MojangsonParser;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class ChatComponent implements ChatComponentProxy {
public final class ChatComponent implements ChatComponentProxy {
@Override
public Object modifyComponent(Object object) {
public Object modifyComponent(@NotNull final Object object) {
if (!(object instanceof IChatBaseComponent)) {
return object;
}
@ -31,7 +32,7 @@ public class ChatComponent implements ChatComponentProxy {
return chatComponent;
}
public void modifyBaseComponent(IChatBaseComponent component) {
private void modifyBaseComponent(@NotNull final IChatBaseComponent component) {
component.getSiblings().forEach(this::modifyBaseComponent);
if (component instanceof ChatMessage) {
Arrays.stream(((ChatMessage) component).getArgs())
@ -68,7 +69,7 @@ public class ChatComponent implements ChatComponentProxy {
((ChatBaseComponent) component).setChatModifier(modifier);
}
private static ItemStack getFromTag(String jsonTag, String id) {
private static ItemStack getFromTag(@NotNull String jsonTag, @NotNull String id) {
id = id.replace("minecraft:", "");
id = id.toUpperCase();
id = id.replace("\"", "");
@ -89,7 +90,7 @@ public class ChatComponent implements ChatComponentProxy {
return CraftItemStack.asBukkitCopy(nmsStack);
}
private static String toJson(ItemStack itemStack) {
private static String toJson(@NotNull final ItemStack itemStack) {
return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString();
}
}

View File

@ -2,10 +2,11 @@ package com.willfp.eco.core.proxy.v1_16_R2;
import com.willfp.eco.core.proxy.proxies.CooldownProxy;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class Cooldown implements CooldownProxy {
public final class Cooldown implements CooldownProxy {
@Override
public double getAttackCooldown(Player player) {
public double getAttackCooldown(@NotNull final Player player) {
return player.getAttackCooldown();
}
}

View File

@ -8,13 +8,14 @@ import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R2.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public class FastGetEnchants implements FastGetEnchantsProxy {
public final class FastGetEnchants implements FastGetEnchantsProxy {
@Override
public Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack) {
public Map<Enchantment, Integer> getEnchantmentsOnItem(@NotNull final ItemStack itemStack) {
net.minecraft.server.v1_16_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
HashMap<Enchantment, Integer> foundEnchantments = new HashMap<>();
@ -31,7 +32,8 @@ public class FastGetEnchants implements FastGetEnchantsProxy {
}
@Override
public int getLevelOnItem(ItemStack itemStack, Enchantment enchantment) {
public int getLevelOnItem(@NotNull final ItemStack itemStack,
@NotNull final Enchantment enchantment) {
net.minecraft.server.v1_16_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();

View File

@ -3,10 +3,11 @@ package com.willfp.eco.core.proxy.v1_16_R2;
import com.willfp.eco.core.proxy.proxies.OpenInventoryProxy;
import org.bukkit.craftbukkit.v1_16_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class OpenInventory implements OpenInventoryProxy {
public final class OpenInventory implements OpenInventoryProxy {
@Override
public Object getOpenInventory(Player player) {
public Object getOpenInventory(@NotNull final Player player) {
return ((CraftPlayer) player).getHandle().activeContainer;
}
}

View File

@ -3,17 +3,19 @@ package com.willfp.eco.core.proxy.v1_16_R2;
import com.willfp.eco.core.proxy.proxies.RepairCostProxy;
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class RepairCost implements RepairCostProxy {
public final class RepairCost implements RepairCostProxy {
@Override
public ItemStack setRepairCost(ItemStack itemStack, int cost) {
public ItemStack setRepairCost(@NotNull final ItemStack itemStack,
final int cost) {
net.minecraft.server.v1_16_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
nmsStack.setRepairCost(cost);
return CraftItemStack.asBukkitCopy(nmsStack);
}
@Override
public int getRepairCost(ItemStack itemStack) {
public int getRepairCost(@NotNull final ItemStack itemStack) {
net.minecraft.server.v1_16_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
return nmsStack.getRepairCost();
}

View File

@ -6,10 +6,11 @@ import org.bukkit.craftbukkit.v1_16_R2.entity.CraftTrident;
import org.bukkit.craftbukkit.v1_16_R2.inventory.CraftItemStack;
import org.bukkit.entity.Trident;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class TridentStack implements TridentStackProxy {
public final class TridentStack implements TridentStackProxy {
@Override
public ItemStack getTridentStack(Trident trident) {
public ItemStack getTridentStack(@NotNull final Trident trident) {
EntityThrownTrident t = ((CraftTrident) trident).getHandle();
return CraftItemStack.asBukkitCopy(t.trident);
}

View File

@ -5,10 +5,12 @@ import net.minecraft.server.v1_16_R3.BlockPosition;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class BlockBreak implements BlockBreakProxy {
public final class BlockBreak implements BlockBreakProxy {
@Override
public void breakBlock(Player player, Block block) {
public void breakBlock(@NotNull final Player player,
@NotNull final Block block) {
((CraftPlayer) player).getHandle().playerInteractManager.breakBlock(new BlockPosition(block.getX(), block.getY(), block.getZ()));
}
}

View File

@ -15,12 +15,13 @@ import net.minecraft.server.v1_16_R3.MojangsonParser;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class ChatComponent implements ChatComponentProxy {
public final class ChatComponent implements ChatComponentProxy {
@Override
public Object modifyComponent(Object object) {
public Object modifyComponent(@NotNull final Object object) {
if (!(object instanceof IChatBaseComponent)) {
return object;
}
@ -31,7 +32,7 @@ public class ChatComponent implements ChatComponentProxy {
return chatComponent;
}
public void modifyBaseComponent(IChatBaseComponent component) {
private void modifyBaseComponent(@NotNull final IChatBaseComponent component) {
component.getSiblings().forEach(this::modifyBaseComponent);
if (component instanceof ChatMessage) {
Arrays.stream(((ChatMessage) component).getArgs())
@ -68,7 +69,7 @@ public class ChatComponent implements ChatComponentProxy {
((ChatBaseComponent) component).setChatModifier(modifier);
}
private static ItemStack getFromTag(String jsonTag, String id) {
private static ItemStack getFromTag(@NotNull String jsonTag, @NotNull String id) {
id = id.replace("minecraft:", "");
id = id.toUpperCase();
id = id.replace("\"", "");
@ -89,7 +90,7 @@ public class ChatComponent implements ChatComponentProxy {
return CraftItemStack.asBukkitCopy(nmsStack);
}
private static String toJson(ItemStack itemStack) {
private static String toJson(@NotNull final ItemStack itemStack) {
return CraftItemStack.asNMSCopy(itemStack).getOrCreateTag().toString();
}
}

View File

@ -2,10 +2,11 @@ package com.willfp.eco.core.proxy.v1_16_R3;
import com.willfp.eco.core.proxy.proxies.CooldownProxy;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class Cooldown implements CooldownProxy {
public final class Cooldown implements CooldownProxy {
@Override
public double getAttackCooldown(Player player) {
public double getAttackCooldown(@NotNull final Player player) {
return player.getAttackCooldown();
}
}

View File

@ -8,13 +8,14 @@ import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public class FastGetEnchants implements FastGetEnchantsProxy {
public final class FastGetEnchants implements FastGetEnchantsProxy {
@Override
public Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack) {
public Map<Enchantment, Integer> getEnchantmentsOnItem(@NotNull final ItemStack itemStack) {
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();
HashMap<Enchantment, Integer> foundEnchantments = new HashMap<>();
@ -31,7 +32,8 @@ public class FastGetEnchants implements FastGetEnchantsProxy {
}
@Override
public int getLevelOnItem(ItemStack itemStack, Enchantment enchantment) {
public int getLevelOnItem(@NotNull final ItemStack itemStack,
@NotNull final Enchantment enchantment) {
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
NBTTagList enchantmentNBT = nmsStack.getEnchantments();

View File

@ -3,10 +3,11 @@ package com.willfp.eco.core.proxy.v1_16_R3;
import com.willfp.eco.core.proxy.proxies.OpenInventoryProxy;
import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class OpenInventory implements OpenInventoryProxy {
public final class OpenInventory implements OpenInventoryProxy {
@Override
public Object getOpenInventory(Player player) {
public Object getOpenInventory(@NotNull final Player player) {
return ((CraftPlayer) player).getHandle().activeContainer;
}
}

View File

@ -3,17 +3,19 @@ package com.willfp.eco.core.proxy.v1_16_R3;
import com.willfp.eco.core.proxy.proxies.RepairCostProxy;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class RepairCost implements RepairCostProxy {
public final class RepairCost implements RepairCostProxy {
@Override
public ItemStack setRepairCost(ItemStack itemStack, int cost) {
public ItemStack setRepairCost(@NotNull final ItemStack itemStack,
final int cost) {
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
nmsStack.setRepairCost(cost);
return CraftItemStack.asBukkitCopy(nmsStack);
}
@Override
public int getRepairCost(ItemStack itemStack) {
public int getRepairCost(@NotNull final ItemStack itemStack) {
net.minecraft.server.v1_16_R3.ItemStack nmsStack = CraftItemStack.asNMSCopy(itemStack);
return nmsStack.getRepairCost();
}

View File

@ -6,10 +6,11 @@ import org.bukkit.craftbukkit.v1_16_R3.entity.CraftTrident;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.entity.Trident;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
public class TridentStack implements TridentStackProxy {
public final class TridentStack implements TridentStackProxy {
@Override
public ItemStack getTridentStack(Trident trident) {
public ItemStack getTridentStack(@NotNull final Trident trident) {
EntityThrownTrident t = ((CraftTrident) trident).getHandle();
return CraftItemStack.asBukkitCopy(t.trident);
}

View File

@ -3,5 +3,8 @@ package com.willfp.eco.core.proxy;
import org.bukkit.Bukkit;
public class ProxyConstants {
/**
* The NMS version that the server is running on.
*/
public static final String NMS_VERSION = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
}

View File

@ -1,21 +1,39 @@
package com.willfp.eco.core.proxy;
import org.jetbrains.annotations.NotNull;
import java.util.IdentityHashMap;
import java.util.Map;
public class ProxyFactory<T extends AbstractProxy> {
private static final Map<Class<? extends AbstractProxy>, Object> CACHE = new IdentityHashMap<>();
/**
* Cached proxy implementations in order to not perform expensive reflective class-finding.
*/
private static final Map<Class<? extends AbstractProxy>, AbstractProxy> CACHE = new IdentityHashMap<>();
/**
* The class of the proxy interface.
*/
private final Class<T> proxyClass;
/**
* Create a new Proxy Factory for a specific type.
*
* @param proxyClass The class of the proxy interface.
*/
public ProxyFactory(Class<T> proxyClass) {
this.proxyClass = proxyClass;
}
public T getProxy() {
/**
* Get the implementation of a proxy.
*
* @return The proxy implementation.
*/
public @NotNull T getProxy() {
try {
T cachedProxy = attemptCache();
if(cachedProxy != null) return cachedProxy;
if (cachedProxy != null) return cachedProxy;
String className = "com.willfp.eco.core.proxy." + ProxyConstants.NMS_VERSION + "." + proxyClass.getSimpleName().replace("Proxy", "");
final Class<?> class2 = Class.forName(className);
@ -34,9 +52,9 @@ public class ProxyFactory<T extends AbstractProxy> {
private T attemptCache() {
Object proxy = CACHE.get(proxyClass);
if(proxy == null) return null;
if (proxy == null) return null;
if(proxyClass.isInstance(proxy)) {
if (proxyClass.isInstance(proxy)) {
return proxyClass.cast(proxy);
}

View File

@ -1,7 +1,14 @@
package com.willfp.eco.core.proxy;
import org.jetbrains.annotations.NotNull;
public class UnsupportedVersionException extends RuntimeException {
public UnsupportedVersionException(String message) {
/**
* Thrown if the server is running an unsupported NMS version.
*
* @param message The message to send.
*/
public UnsupportedVersionException(@NotNull final String message) {
super(message);
}
}

View File

@ -4,10 +4,15 @@ package com.willfp.eco.core.proxy.proxies;
import com.willfp.eco.core.proxy.AbstractProxy;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
/**
* Utility class to break a block as if the player had done it manually
*/
public interface BlockBreakProxy extends AbstractProxy {
void breakBlock(Player player, Block block);
/**
* Break the block as if the player had done it manually.
*
* @param player The player to break the block as.
* @param block The block to break.
*/
void breakBlock(@NotNull Player player,
@NotNull Block block);
}

View File

@ -2,10 +2,13 @@ package com.willfp.eco.core.proxy.proxies;
import com.willfp.eco.core.proxy.AbstractProxy;
import org.jetbrains.annotations.NotNull;
/**
* Utility class to manage chat components
*/
public interface ChatComponentProxy extends AbstractProxy {
Object modifyComponent(Object object);
/**
* Modify hover {@link org.bukkit.inventory.ItemStack}s using EnchantDisplay#displayEnchantments.
* @param object The NMS ChatComponent to modify.
* @return The modified ChatComponent.
*/
Object modifyComponent(@NotNull Object object);
}

View File

@ -3,10 +3,14 @@ package com.willfp.eco.core.proxy.proxies;
import com.willfp.eco.core.proxy.AbstractProxy;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
/**
* Utility class to get the attack cooldown of a player
*/
public interface CooldownProxy extends AbstractProxy {
double getAttackCooldown(Player player);
/**
* Get the attack cooldown for a player.
*
* @param player The player's attack cooldown.
* @return A value between 0 and 1, with 1 representing full power.
*/
double getAttackCooldown(@NotNull Player player);
}

View File

@ -3,13 +3,25 @@ package com.willfp.eco.core.proxy.proxies;
import com.willfp.eco.core.proxy.AbstractProxy;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
/**
* Utility class to break a block as if the player had done it manually
*/
public interface FastGetEnchantsProxy extends AbstractProxy {
Map<Enchantment, Integer> getEnchantmentsOnItem(ItemStack itemStack);
int getLevelOnItem(ItemStack itemStack, Enchantment enchantment);
/**
* Get all enchantments on an {@link ItemStack}.
*
* @param itemStack The item to query.
* @return A map of all enchantments, where the value represents the level present.
*/
Map<Enchantment, Integer> getEnchantmentsOnItem(@NotNull ItemStack itemStack);
/**
* Get the level of a specified enchantment on an item.
* @param itemStack The item to query.
* @param enchantment The enchantment to query.
* @return The level found, or 0 if not present.
*/
int getLevelOnItem(@NotNull ItemStack itemStack,
@NotNull Enchantment enchantment);
}

View File

@ -2,10 +2,13 @@ package com.willfp.eco.core.proxy.proxies;
import com.willfp.eco.core.proxy.AbstractProxy;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
/**
* Utility class to get the NMS implementation of a players' currently open inventory
*/
public interface OpenInventoryProxy extends AbstractProxy {
Object getOpenInventory(Player player);
/**
* Get the NMS inventory container for a player's inventory view.
* @param player The player to query.
* @return The NMS inventory container.
*/
Object getOpenInventory(@NotNull Player player);
}

View File

@ -2,11 +2,24 @@ package com.willfp.eco.core.proxy.proxies;
import com.willfp.eco.core.proxy.AbstractProxy;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* Utility class to get and set the anvil rework penalty of an item
*/
public interface RepairCostProxy extends AbstractProxy {
ItemStack setRepairCost(ItemStack itemStack, int cost);
int getRepairCost(ItemStack itemStack);
/**
* Set the rework penalty of an item.
*
* @param itemStack The item to query.
* @param cost The rework penalty to set.
* @return The item, with the rework penalty applied.
*/
ItemStack setRepairCost(@NotNull ItemStack itemStack,
int cost);
/**
* Get the rework penalty of an item.
*
* @param itemStack The item to query.
* @return The rework penalty found on the item.
*/
int getRepairCost(@NotNull ItemStack itemStack);
}

View File

@ -4,10 +4,14 @@ package com.willfp.eco.core.proxy.proxies;
import com.willfp.eco.core.proxy.AbstractProxy;
import org.bukkit.entity.Trident;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
/**
* Utility class to get the {@link ItemStack} of a given {@link Trident}
*/
public interface TridentStackProxy extends AbstractProxy {
ItemStack getTridentStack(Trident trident);
/**
* Get a trident's ItemStack.
*
* @param trident The trident to query.
* @return The trident's ItemStack.
*/
ItemStack getTridentStack(@NotNull Trident trident);
}

View File

@ -2,9 +2,18 @@ package com.willfp.eco.util;
import com.willfp.eco.core.proxy.AbstractProxy;
import com.willfp.eco.core.proxy.ProxyFactory;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.NotNull;
@UtilityClass
public class ProxyUtils {
public static <T extends AbstractProxy> T getProxy(Class<T> proxyClass) {
/**
* Get the implementation of a specified proxy.
* @param proxyClass The proxy interface.
* @param <T> The type of the proxy.
* @return The proxy implementation.
*/
public @NotNull <T extends AbstractProxy> T getProxy(@NotNull final Class<T> proxyClass) {
return new ProxyFactory<>(proxyClass).getProxy();
}
}