Dateien nach "src/main/java/com/viper/PlaceholderSIGN/command" hochladen
This commit is contained in:
139
src/main/java/com/viper/PlaceholderSIGN/command/SignCommand.java
Normal file
139
src/main/java/com/viper/PlaceholderSIGN/command/SignCommand.java
Normal file
@@ -0,0 +1,139 @@
|
||||
package com.viper.PlaceholderSIGN.command;
|
||||
|
||||
import com.viper.PlaceholderSIGN.Main;
|
||||
import com.viper.PlaceholderSIGN.sign.PlaceholderSign;
|
||||
import com.viper.PlaceholderSIGN.util.HexUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class SignCommand implements CommandExecutor, Listener {
|
||||
private final List<UserData> ud;
|
||||
|
||||
public SignCommand() {
|
||||
this.ud = new ArrayList<>();
|
||||
PluginCommand command = Main.getInstance().getCommand("editsign");
|
||||
Objects.requireNonNull(command).setExecutor(this);
|
||||
command.setAliases(new ArrayList<>(Collections.singletonList("es")));
|
||||
Bukkit.getPluginManager().registerEvents(this, (Plugin) Main.getInstance());
|
||||
}
|
||||
|
||||
private static class UserData {
|
||||
public Player p;
|
||||
public String txt;
|
||||
public int line;
|
||||
|
||||
public UserData(Player p, String txt, int line) {
|
||||
this.p = p;
|
||||
this.txt = txt;
|
||||
this.line = line;
|
||||
}
|
||||
}
|
||||
|
||||
public UserData getData(Player p) {
|
||||
for (UserData u : this.ud) {
|
||||
if (u.p == p) {
|
||||
return u;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage("[PlaceholderSign] Console can't edit signs! [ERROR]");
|
||||
return true;
|
||||
}
|
||||
Player player = (Player) sender;
|
||||
if (!player.hasPermission("placeholdersign.editsign")) {
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.noperm));
|
||||
return true;
|
||||
}
|
||||
if (args.length < 2) {
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.usage));
|
||||
return true;
|
||||
}
|
||||
int line;
|
||||
try {
|
||||
line = Integer.parseInt(args[0]);
|
||||
} catch (NumberFormatException e) {
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.numbers));
|
||||
return true;
|
||||
}
|
||||
if (line < 1 || line > 4) {
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.numbers));
|
||||
return true;
|
||||
}
|
||||
StringBuilder txt = new StringBuilder(args[1]);
|
||||
for (int i = 2; i < args.length; i++) {
|
||||
txt.append(" ").append(args[i]);
|
||||
}
|
||||
String text = txt.toString();
|
||||
if (Main.getInstance().hasPAPI()) {
|
||||
text = PlaceholderAPI.setPlaceholders(player, text);
|
||||
}
|
||||
if (Main.getInstance().hasRGB()) {
|
||||
text = HexUtil.replaceHexColors('&', text);
|
||||
}
|
||||
if (getData(player) != null) {
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.abort));
|
||||
this.ud.remove(getData(player));
|
||||
return true;
|
||||
}
|
||||
this.ud.add(new UserData(player, text, line));
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.click));
|
||||
return true;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onClickSign(PlayerInteractEvent e) {
|
||||
if ((e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) &&
|
||||
e.getClickedBlock() != null && e.getClickedBlock().getState() instanceof Sign) {
|
||||
Player player = e.getPlayer();
|
||||
UserData data = getData(player);
|
||||
if (data != null) {
|
||||
Sign s = (Sign) e.getClickedBlock().getState();
|
||||
PlaceholderSign ps = Main.getPSManager().getPlaceholderSign(s);
|
||||
if (ps == null) {
|
||||
List<String> temp = new ArrayList<>(Arrays.asList(s.getLines()));
|
||||
while (temp.size() != 4) {
|
||||
temp.add("");
|
||||
}
|
||||
Main.getPSManager().createSign(s, temp);
|
||||
ps = Main.getPSManager().getPlaceholderSign(s);
|
||||
}
|
||||
s.setLine(data.line - 1, data.txt);
|
||||
List<String> txt = new ArrayList<>();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (i == data.line - 1) {
|
||||
txt.add(data.txt);
|
||||
} else {
|
||||
txt.add(ps.getLinesRaw()[i]);
|
||||
}
|
||||
}
|
||||
ps.setLinesRaw(txt.toArray(new String[0]));
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.update));
|
||||
this.ud.remove(data);
|
||||
s.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user