|
| 1 | +package ru.xpyctum.SignStatus; |
| 2 | + |
| 3 | +import cn.nukkit.Player; |
| 4 | +import cn.nukkit.block.Block; |
| 5 | +import cn.nukkit.blockentity.BlockEntitySign; |
| 6 | +import cn.nukkit.event.EventHandler; |
| 7 | +import cn.nukkit.event.Listener; |
| 8 | +import cn.nukkit.event.block.BlockBreakEvent; |
| 9 | +import cn.nukkit.event.block.SignChangeEvent; |
| 10 | +import cn.nukkit.item.Item; |
| 11 | +import cn.nukkit.level.Level; |
| 12 | +import cn.nukkit.math.Vector3; |
| 13 | +import cn.nukkit.plugin.PluginBase; |
| 14 | +import cn.nukkit.utils.Config; |
| 15 | +import cn.nukkit.utils.ConfigSection; |
| 16 | + |
| 17 | +import java.util.*; |
| 18 | + |
| 19 | +/** |
| 20 | + ████████████████████████████████████████████ |
| 21 | + █──█──██────██──█──██────██───██─█─██─███─██ |
| 22 | + ██───███─██─███───███─██─███─███─█─██──█──██ |
| 23 | + ███─████────████─████─██████─███─█─██─█─█─██ |
| 24 | + ██───███─███████─████─██─███─███─█─██─███─██ |
| 25 | + █──█──██─███████─████────███─███───██─███─██ |
| 26 | + ████████████████████████████████████████████ |
| 27 | + */ |
| 28 | +public class Main extends PluginBase implements Listener{ |
| 29 | + |
| 30 | + public Config tranlations; |
| 31 | + public Config config; |
| 32 | + public Config format; |
| 33 | + public Config signs; |
| 34 | + |
| 35 | + @Override |
| 36 | + public void onEnable() { |
| 37 | + getDataFolder().mkdir(); |
| 38 | + |
| 39 | + this.saveResource("config.yml"); |
| 40 | + this.saveResource("format.yml"); |
| 41 | + this.saveResource("translations.yml"); |
| 42 | + this.saveResource("signs.yml"); |
| 43 | + |
| 44 | + this.tranlations = new Config(this.getDataFolder()+"/translations.yml"); |
| 45 | + this.config = new Config(this.getDataFolder()+"/config.yml"); |
| 46 | + this.format = new Config(this.getDataFolder()+"/format.yml"); |
| 47 | + this.signs = new Config(this.getDataFolder()+"/signs.yml"); |
| 48 | + |
| 49 | + getServer().getPluginManager().registerEvents(this, this); |
| 50 | + |
| 51 | + this.getServer().getScheduler().scheduleRepeatingTask(new Refresher(this), config.getInt("time")*20); |
| 52 | + |
| 53 | + getLogger().info("SignStatus successfully loaded!"); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onDisable(){ |
| 58 | + getLogger().info("SignStatus successfully disabled!"); |
| 59 | + } |
| 60 | + |
| 61 | + @EventHandler |
| 62 | + public void onSignChange(SignChangeEvent event) { |
| 63 | + Player player = event.getPlayer(); |
| 64 | + if(Objects.equals(event.getLine(0).toLowerCase(), "status") || Objects.equals(event.getLine(0).toLowerCase(), "[status]")){ |
| 65 | + if(player.hasPermission("signstatus") || player.hasPermission("signstatus.create")){ |
| 66 | + ConfigSection data = format.getSection("format"); |
| 67 | + |
| 68 | + data.entrySet().forEach(entry ->{ |
| 69 | + int line = Integer.parseInt(entry.getKey()); |
| 70 | + String formattedText = formatedRow(entry.getValue().toString(),player.getLevel()); |
| 71 | + event.setLine(line-1,formattedText); |
| 72 | + }); |
| 73 | + player.sendMessage(tranlations.getString("sign_created")); |
| 74 | + |
| 75 | + long unixTime = System.currentTimeMillis() / 1000L; |
| 76 | + List<Object> listObj = new ArrayList<>(); |
| 77 | + |
| 78 | + listObj.add(new LinkedHashMap<String, Object>() { |
| 79 | + { |
| 80 | + put("z", event.getBlock().getZ()); |
| 81 | + put("y", event.getBlock().getY()); |
| 82 | + put("x", event.getBlock().getX()); |
| 83 | + put("level-name", player.getLevel().getName()); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + signs.set("sign"+Long.toString(unixTime),listObj); |
| 88 | + signs.save(); |
| 89 | + }else{ |
| 90 | + player.sendMessage(tranlations.getString("sign_no_perms")); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @EventHandler |
| 96 | + public void onBlockBreak(BlockBreakEvent event){ |
| 97 | + Player player = event.getPlayer(); |
| 98 | + Block block = event.getBlock(); |
| 99 | + if(block.getId() == Block.SIGN_POST || block.getId() == Block.WALL_SIGN || block.getId() == Item.SIGN){ |
| 100 | + BlockEntitySign blockEntitySign = (BlockEntitySign) block.getLevel().getBlockEntity(new Vector3(block.getX(),block.getY(),block.getZ())); |
| 101 | + if(blockEntitySign != null){ |
| 102 | + if(this.isCreated(new Vector3(block.getX(),block.getY(),block.getZ()))){ |
| 103 | + if(player.hasPermission("signstatus.break")) { |
| 104 | + this.remove(new Vector3(block.getX(), block.getY(), block.getZ())); |
| 105 | + player.sendMessage(tranlations.getString("sign_destroyed")); |
| 106 | + }else{ |
| 107 | + player.sendMessage(tranlations.getString("sign_no_perms")); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public void remove(Vector3 pos){ |
| 115 | + ConfigSection signs = this.signs.getRootSection(); |
| 116 | + for(Map.Entry entry : signs.entrySet()){ |
| 117 | + List<Map> cf = signs.getMapList(entry.getKey().toString()); |
| 118 | + for(Map joinable : cf){ |
| 119 | + double x = Double.parseDouble(joinable.get("x").toString()); |
| 120 | + double y = Double.parseDouble(joinable.get("y").toString()); |
| 121 | + double z = Double.parseDouble(joinable.get("z").toString()); |
| 122 | + String level_name = (String) joinable.get("level-name"); |
| 123 | + Level level = this.getServer().getLevelByName(level_name); |
| 124 | + if(level != null) { |
| 125 | + if (x == pos.getX() && y == pos.getY() && z == pos.getZ() && Objects.equals(level.getName(), level_name)){ |
| 126 | + this.getLogger().info(entry.getKey().toString()); |
| 127 | + signs.remove(entry.getKey().toString()); |
| 128 | + this.signs.save(); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public boolean isCreated(Vector3 pos){ |
| 136 | + ConfigSection signs = this.signs.getRootSection(); |
| 137 | + for(Map.Entry entry : signs.entrySet()){ |
| 138 | + List<Map> cf = signs.getMapList(entry.getKey().toString()); |
| 139 | + for(Map joinable : cf){ |
| 140 | + double x = Double.parseDouble(joinable.get("x").toString()); |
| 141 | + double y = Double.parseDouble(joinable.get("y").toString()); |
| 142 | + double z = Double.parseDouble(joinable.get("z").toString()); |
| 143 | + String level_name = (String) joinable.get("level-name"); |
| 144 | + Level level = this.getServer().getLevelByName(level_name); |
| 145 | + if(level != null) { |
| 146 | + if (x == pos.getX() && y == pos.getY() && z == pos.getZ() && Objects.equals(level.getName(), level_name)){ |
| 147 | + return true; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + public String formatedRow(String line, Level level){ |
| 156 | + float tps = getServer().getTicksPerSecond(); |
| 157 | + int players = getServer().getOnlinePlayers().size(); |
| 158 | + int max_online = getServer().getMaxPlayers(); |
| 159 | + float load = getServer().getTickUsage(); |
| 160 | + String world_name = level.getName(); |
| 161 | + |
| 162 | + line = line.replace("{ONLINE}",String.valueOf(players)); |
| 163 | + line = line.replace("{MAX_ONLINE}",String.valueOf(max_online)); |
| 164 | + line = line.replace("{WORLD_NAME}",world_name); |
| 165 | + line = line.replace("{TPS}",String.valueOf(tps)); |
| 166 | + line = line.replace("{SERVER_LOAD}",String.valueOf(load)); |
| 167 | + |
| 168 | + return line; |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | +} |
0 commit comments