Added player option to display, added setters to commands

This commit is contained in:
Auxilor
2021-07-27 18:48:10 +01:00
parent b6086bc4bd
commit 364f36d502
14 changed files with 112 additions and 31 deletions

View File

@@ -42,10 +42,24 @@ public interface CommandBase {
*/
CommandHandler getHandler();
/**
* Set the handler.
*
* @param handler The handler.
*/
void setHandler(@NotNull CommandHandler handler);
/**
* Get the tab completer.
*
* @return The tab completer.
*/
TabCompleteHandler getTabCompleter();
/**
* Set the tab completer.
*
* @param handler The handler.
*/
void setTabCompleter(@NotNull TabCompleteHandler handler);
}

View File

@@ -7,6 +7,7 @@ import com.willfp.eco.core.command.CommandHandler;
import com.willfp.eco.core.command.TabCompleteHandler;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
@@ -48,6 +49,20 @@ abstract class HandledCommand extends PluginDependent<EcoPlugin> implements Comm
@Getter
private final boolean playersOnly;
/**
* The actual code to be executed in the command.
*/
@Getter
@Setter
private CommandHandler handler = (sender, args) -> { };
/**
* The tab completion code to be executed in the command.
*/
@Getter
@Setter
private TabCompleteHandler tabCompleter = (sender, args) -> new ArrayList<>();
/**
* All subcommands for the command.
*/
@@ -164,14 +179,6 @@ abstract class HandledCommand extends PluginDependent<EcoPlugin> implements Comm
return this.getTabCompleter().tabComplete(sender, Arrays.asList(args));
}
@Override
public abstract CommandHandler getHandler();
@Override
public TabCompleteHandler getTabCompleter() {
return (sender, args) -> new ArrayList<>();
}
/**
* If a sender can execute the command.
*

View File

@@ -3,12 +3,14 @@ package com.willfp.eco.core.display;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang.Validate;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
@@ -57,6 +59,18 @@ public class Display {
* @return The ItemStack.
*/
public ItemStack display(@NotNull final ItemStack itemStack) {
return display(itemStack, null);
}
/**
* Display on ItemStacks.
*
* @param itemStack The item.
* @param player The player.
* @return The ItemStack.
*/
public ItemStack display(@NotNull final ItemStack itemStack,
@Nullable final Player player) {
if (!itemStack.hasItemMeta()) {
return itemStack; // return early if there's no customization of the item
}
@@ -83,6 +97,9 @@ public class Display {
for (DisplayModule module : modules) {
Object[] varargs = pluginVarArgs.get(module.getPluginName());
module.display(itemStack, varargs);
if (player != null) {
module.display(itemStack, player, varargs);
}
}
}
@@ -96,7 +113,19 @@ public class Display {
* @return The ItemStack.
*/
public ItemStack displayAndFinalize(@NotNull final ItemStack itemStack) {
return finalize(display(itemStack));
return finalize(display(itemStack, null));
}
/**
* Display on ItemStacks and then finalize.
*
* @param itemStack The item.
* @param player The player.
* @return The ItemStack.
*/
public ItemStack displayAndFinalize(@NotNull final ItemStack itemStack,
@Nullable final Player player) {
return finalize(display(itemStack, player));
}
/**

View File

@@ -3,6 +3,7 @@ package com.willfp.eco.core.display;
import com.willfp.eco.core.EcoPlugin;
import com.willfp.eco.core.PluginDependent;
import lombok.Getter;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -39,6 +40,19 @@ public abstract class DisplayModule extends PluginDependent<EcoPlugin> {
// Technically optional.
}
/**
* Display an item.
*
* @param itemStack The item.
* @param player The player.
* @param args Optional args for display.
*/
protected void display(@NotNull final ItemStack itemStack,
@NotNull final Player player,
@NotNull final Object... args) {
// Technically optional.
}
/**
* Revert an item.
*

View File

@@ -14,6 +14,7 @@ import net.minecraft.server.v1_16_R3.IChatBaseComponent;
import net.minecraft.server.v1_16_R3.MojangsonParser;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -21,7 +22,8 @@ import java.util.Arrays;
public final class ChatComponent implements ChatComponentProxy {
@Override
public Object modifyComponent(@NotNull final Object object) {
public Object modifyComponent(@NotNull final Object object,
@NotNull final Player player) {
if (!(object instanceof IChatBaseComponent chatComponent)) {
return object;
}
@@ -31,25 +33,26 @@ public final class ChatComponent implements ChatComponentProxy {
continue;
}
modifyBaseComponent(iChatBaseComponent);
modifyBaseComponent(iChatBaseComponent, player);
}
return chatComponent;
}
private void modifyBaseComponent(@NotNull final IChatBaseComponent component) {
private void modifyBaseComponent(@NotNull final IChatBaseComponent component,
@NotNull final Player player) {
for (IChatBaseComponent sibling : component.getSiblings()) {
if (sibling == null) {
continue;
}
modifyBaseComponent(sibling);
modifyBaseComponent(sibling, player);
}
if (component instanceof ChatMessage) {
Arrays.stream(((ChatMessage) component).getArgs())
.filter(o -> o instanceof IChatBaseComponent)
.map(o -> (IChatBaseComponent) o)
.forEach(this::modifyBaseComponent);
.forEach(o -> this.modifyBaseComponent(o, player));
}
ChatHoverable hoverable = component.getChatModifier().getHoverEvent();
@@ -70,7 +73,7 @@ public final class ChatComponent implements ChatComponentProxy {
String tag = json.getAsJsonObject().get("tag").toString();
ItemStack itemStack = getFromTag(tag, id);
Display.displayAndFinalize(itemStack);
Display.displayAndFinalize(itemStack, player);
json.getAsJsonObject().remove("tag");
String newTag = toJson(itemStack);

View File

@@ -3,6 +3,7 @@ package com.willfp.eco.proxy.v1_16_R3;
import com.willfp.eco.core.display.Display;
import com.willfp.eco.proxy.VillagerTradeProxy;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftMerchantRecipe;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import org.jetbrains.annotations.NotNull;
@@ -25,7 +26,8 @@ public final class VillagerTrade implements VillagerTradeProxy {
}
@Override
public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) {
public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe,
@NotNull final Player player) {
CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe;
CraftMerchantRecipe newRecipe = new CraftMerchantRecipe(
@@ -38,7 +40,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
);
for (ItemStack ingredient : recipe.getIngredients()) {
newRecipe.addIngredient(Display.display(ingredient.clone()));
newRecipe.addIngredient(Display.display(ingredient.clone(), player));
}
getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice());

View File

@@ -14,6 +14,7 @@ import net.minecraft.network.chat.ChatModifier;
import net.minecraft.network.chat.IChatBaseComponent;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
@@ -21,7 +22,8 @@ import java.util.Arrays;
public final class ChatComponent implements ChatComponentProxy {
@Override
public Object modifyComponent(@NotNull final Object object) {
public Object modifyComponent(@NotNull final Object object,
@NotNull final Player player) {
if (!(object instanceof IChatBaseComponent chatComponent)) {
return object;
}
@@ -31,25 +33,26 @@ public final class ChatComponent implements ChatComponentProxy {
continue;
}
modifyBaseComponent(iChatBaseComponent);
modifyBaseComponent(iChatBaseComponent, player);
}
return chatComponent;
}
private void modifyBaseComponent(@NotNull final IChatBaseComponent component) {
private void modifyBaseComponent(@NotNull final IChatBaseComponent component,
@NotNull final Player player) {
for (IChatBaseComponent sibling : component.getSiblings()) {
if (sibling == null) {
continue;
}
modifyBaseComponent(sibling);
modifyBaseComponent(sibling, player);
}
if (component instanceof ChatMessage) {
Arrays.stream(((ChatMessage) component).getArgs())
.filter(o -> o instanceof IChatBaseComponent)
.map(o -> (IChatBaseComponent) o)
.forEach(this::modifyBaseComponent);
.forEach(o -> modifyBaseComponent(o, player));
}
ChatHoverable hoverable = component.getChatModifier().getHoverEvent();
@@ -70,7 +73,7 @@ public final class ChatComponent implements ChatComponentProxy {
String tag = json.getAsJsonObject().get("tag").toString();
ItemStack itemStack = getFromTag(tag, id);
Display.displayAndFinalize(itemStack);
Display.displayAndFinalize(itemStack, player);
json.getAsJsonObject().remove("tag");
String newTag = toJson(itemStack);

View File

@@ -3,6 +3,7 @@ package com.willfp.eco.proxy.v1_17_R1;
import com.willfp.eco.core.display.Display;
import com.willfp.eco.proxy.VillagerTradeProxy;
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftMerchantRecipe;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.MerchantRecipe;
import org.jetbrains.annotations.NotNull;
@@ -25,7 +26,8 @@ public final class VillagerTrade implements VillagerTradeProxy {
}
@Override
public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe) {
public MerchantRecipe displayTrade(@NotNull final MerchantRecipe recipe,
@NotNull final Player player) {
CraftMerchantRecipe oldRecipe = (CraftMerchantRecipe) recipe;
CraftMerchantRecipe newRecipe = new CraftMerchantRecipe(
@@ -38,7 +40,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
);
for (ItemStack ingredient : recipe.getIngredients()) {
newRecipe.addIngredient(Display.display(ingredient.clone()));
newRecipe.addIngredient(Display.display(ingredient.clone(), player));
}
getHandle(newRecipe).setSpecialPrice(getHandle(oldRecipe).getSpecialPrice());

View File

@@ -30,7 +30,7 @@ public class PacketChat extends AbstractPacketAdapter {
return;
}
WrappedChatComponent newComponent = WrappedChatComponent.fromHandle(this.getPlugin().getProxy(ChatComponentProxy.class).modifyComponent(component.getHandle()));
WrappedChatComponent newComponent = WrappedChatComponent.fromHandle(this.getPlugin().getProxy(ChatComponentProxy.class).modifyComponent(component.getHandle(), player));
packet.getChatComponents().write(i, newComponent);
}
}

View File

@@ -47,7 +47,7 @@ public class PacketOpenWindowMerchant extends AbstractPacketAdapter {
}
for (MerchantRecipe recipe : packet.getMerchantRecipeLists().read(0)) {
MerchantRecipe newRecipe = this.getPlugin().getProxy(VillagerTradeProxy.class).displayTrade(recipe);
MerchantRecipe newRecipe = this.getPlugin().getProxy(VillagerTradeProxy.class).displayTrade(recipe, player);
recipes.add(newRecipe);
}

View File

@@ -18,6 +18,6 @@ public class PacketSetSlot extends AbstractPacketAdapter {
public void onSend(@NotNull final PacketContainer packet,
@NotNull final Player player,
@NotNull final PacketEvent event) {
packet.getItemModifier().modify(0, Display::display);
packet.getItemModifier().modify(0, item -> Display.display(item, player));
}
}

View File

@@ -22,7 +22,7 @@ public class PacketWindowItems extends AbstractPacketAdapter {
if (itemStacks == null) {
return null;
}
itemStacks.forEach(Display::display);
itemStacks.forEach(item -> Display.display(item, player));
return itemStacks;
});
}

View File

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

View File

@@ -1,6 +1,7 @@
package com.willfp.eco.proxy;
import com.willfp.eco.core.proxy.AbstractProxy;
import org.bukkit.entity.Player;
import org.bukkit.inventory.MerchantRecipe;
import org.jetbrains.annotations.NotNull;
@@ -9,7 +10,9 @@ public interface VillagerTradeProxy extends AbstractProxy {
* Display a MerchantRecipe.
*
* @param recipe The recipe.
* @param player The player.
* @return The new recipe.
*/
MerchantRecipe displayTrade(@NotNull MerchantRecipe recipe);
MerchantRecipe displayTrade(@NotNull MerchantRecipe recipe,
@NotNull Player player);
}