LibsDisguises/src/main/java/me/libraryaddict/disguise/commands/CopyDisguiseCommand.java
libraryaddict 897a6629ae
Read for information...
Now using ASM manipulation to remove invalid methods on load
Fixed imports
Fixed Chat Components being used in 1.12
Fixed tab complete showing args for disguise options you can't use
Disguise option permissions now demand a parameter to be the method name
Falling block disguises are now only usable with blocks
LibsDisguises command now tab completes the new options
Libs Disguises command lets you create a vanilla compatible item string
If a vehicle is disguised as a vehicle, don't give no gravity
Fixed horse disguise using almost random values for its flagwatcher settings
Renamed horse disguise setMouthOpen to setEating
Slightly better string for premium info jar location
Skip attributes packets if using older ProtocolLib jar
Don't cancel entity death if entity is dead
Improved disguise permissions checking
Fixed time parameter not being attributed properly
2020-02-19 12:57:39 +13:00

159 lines
5.6 KiB
Java

package me.libraryaddict.disguise.commands;
import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.PlayerDisguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsPremium;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.UUID;
/**
* Created by libraryaddict on 1/01/2020.
*/
public class CopyDisguiseCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
if (sender instanceof Player && !sender.isOp() &&
(!LibsPremium.isPremium() || LibsPremium.getPaidInformation() == LibsPremium.getPluginInformation())) {
sender.sendMessage(ChatColor.RED + "Please purchase Lib's Disguises to enable player commands");
return true;
}
if (!sender.hasPermission("libsdisguises.copydisguise")) {
sender.sendMessage(LibsMsg.NO_PERM.get());
return true;
}
Entity target = sender instanceof Player ? (Entity) sender : null;
if (args.length > 0) {
target = Bukkit.getPlayer(args[0]);
if (target == null) {
if (args[0].contains("-")) {
try {
target = Bukkit.getEntity(UUID.fromString(args[0]));
}
catch (Exception ignored) {
}
}
}
if (target == null) {
sender.sendMessage(LibsMsg.CANNOT_FIND_PLAYER.get(args[0]));
return true;
}
}
Disguise disguise = DisguiseAPI.getDisguise(target);
if (disguise == null) {
sender.sendMessage((sender == target ? LibsMsg.NOT_DISGUISED : LibsMsg.TARGET_NOT_DISGUISED).get());
return true;
}
String disguiseString = DisguiseParser.parseToString(disguise, false);
/*if (!(sender instanceof Player)) {
sender.sendMessage(disguiseString);
if (disguise instanceof PlayerDisguise) {
sender.sendMessage(DisguiseParser.parseToString(disguise, false));
}
return true;
}*/
sendMessage(sender, LibsMsg.CLICK_TO_COPY, LibsMsg.COPY_DISGUISE_NO_COPY, disguiseString, false);
if (disguise instanceof PlayerDisguise) {
sendMessage(sender, LibsMsg.CLICK_TO_COPY_WITH_SKIN, LibsMsg.CLICK_TO_COPY_WITH_SKIN_NO_COPY,
DisguiseParser.parseToString(disguise), true);
}
DisguiseUtilities.setCopyDisguiseCommandUsed();
return true;
}
private void sendMessage(CommandSender sender, LibsMsg msg, LibsMsg oldVer, String string, boolean forceAbbrev) {
if (!NmsVersion.v1_13.isSupported()) {
sender.sendMessage(oldVer.get(string));
return;
}
ComponentBuilder builder = new ComponentBuilder("").appendLegacy(msg.get()).append(" ");
if (string.length() > 256 || forceAbbrev) {
String[] split = DisguiseUtilities.split(string);
for (int i = 0; i < split.length; i++) {
if (split[i].length() <= 256) {
continue;
}
split = Arrays.copyOf(split, split.length + 1);
for (int a = split.length - 1; a > i; a--) {
split[a] = split[a - 1];
}
split[i + 1] = split[i].substring(256);
split[i] = split[i].substring(0, 256);
}
int sections = 0;
StringBuilder current = new StringBuilder();
for (int i = 0; i < split.length; i++) {
if (current.length() > 0) {
current.append(" ");
}
current.append(split[i]);
// If the next split would fit
if (split.length > i + 1 && split[i + 1].length() + current.length() + 1 <= 256) {
continue;
}
if (sections != 0) {
builder.append(" ");
builder.reset();
}
sections++;
builder.appendLegacy(LibsMsg.CLICK_COPY.get(sections));
builder.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, current.toString()));
builder.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder(LibsMsg.CLICK_TO_COPY_HOVER.get() + " " + sections).create()));
current = new StringBuilder();
}
} else {
builder.appendLegacy(LibsMsg.CLICK_COPY.get(string));
builder.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, string));
builder.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder(LibsMsg.CLICK_TO_COPY_HOVER.get()).create()));
}
sender.spigot().sendMessage(builder.create());
}
}