Last active
December 26, 2015 20:59
-
-
Save NeatMonster/ef9937ca36fa04e27431 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Wanted | |
version: 1.0 | |
author: MinusKube | |
main: fr.minuskube.wanted.Wanted | |
commands: | |
wanted: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fr.minuskube.wanted; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Map; | |
import java.util.Set; | |
import org.apache.commons.lang.StringUtils; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.Location; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.Sign; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.configuration.serialization.ConfigurationSerializable; | |
import org.bukkit.configuration.serialization.ConfigurationSerialization; | |
import org.bukkit.configuration.serialization.SerializableAs; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.block.BlockBreakEvent; | |
import org.bukkit.event.entity.PlayerDeathEvent; | |
import org.bukkit.plugin.java.JavaPlugin; | |
public class Wanted extends JavaPlugin implements Listener { | |
@SerializableAs("BlockLocation") | |
public static class BlockLocation extends Location implements ConfigurationSerializable { | |
public BlockLocation(final Location location) { | |
super(location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ()); | |
} | |
public BlockLocation(final Map<String, Object> map) { | |
super(Bukkit.getWorld((String) map.get("world")), (int) map.get("x"), (int) map.get("y"), (int) map.get("z")); | |
} | |
@Override | |
public boolean equals(final Object obj) { | |
if (!(obj instanceof Location)) | |
return false; | |
final Location loc = (Location) obj; | |
return loc.getWorld().getName().equals(getWorld().getName()) && loc.getBlockX() == getBlockX() | |
&& loc.getBlockY() == getBlockY() && loc.getBlockZ() == getBlockZ(); | |
} | |
@Override | |
public Map<String, Object> serialize() { | |
final Map<String, Object> map = new HashMap<String, Object>(); | |
map.put("world", getWorld().getName()); | |
map.put("x", getBlockX()); | |
map.put("y", getBlockY()); | |
map.put("z", getBlockZ()); | |
return map; | |
} | |
} | |
private static File INTERNAL = null; | |
private final Set<BlockLocation> signs = new HashSet<BlockLocation>(); | |
@EventHandler(priority = EventPriority.MONITOR) | |
public void onBlockBreak(final BlockBreakEvent event) { | |
final Block block = event.getBlock(); | |
if (block.getState() != null && block.getState() instanceof Sign) { | |
signs.remove(new BlockLocation(block.getLocation())); | |
saveSigns(); | |
} | |
} | |
@Override | |
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) { | |
if (command.getName().equalsIgnoreCase("wanted")) { | |
if (sender instanceof Player) { | |
final Player player = (Player) sender; | |
if (player.hasPermission("wanted.define")) { | |
@SuppressWarnings("deprecation") | |
final Block block = player.getTargetBlock(null, 100); | |
if (block.getState() != null && block.getState() instanceof Sign) { | |
signs.add(new BlockLocation(block.getLocation())); | |
saveSigns(); | |
player.sendMessage(ChatColor.GREEN + "A new wanted sign has been defined."); | |
} else | |
player.sendMessage(ChatColor.RED + "The targetted block isn't a sign."); | |
} else | |
player.sendMessage(ChatColor.RED + "You don't have the permission to do this."); | |
} else | |
sender.sendMessage(ChatColor.RED + "This command can only be used by players."); | |
return true; | |
} | |
return false; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public void onEnable() { | |
ConfigurationSerialization.registerClass(BlockLocation.class); | |
INTERNAL = new File(getDataFolder().getPath(), "internal.yml"); | |
if (!INTERNAL.exists()) | |
try { | |
INTERNAL.createNewFile(); | |
} catch (final IOException e) {} | |
final YamlConfiguration config = YamlConfiguration.loadConfiguration(INTERNAL); | |
signs.clear(); | |
signs.addAll((Set<BlockLocation>) config.get("signs", new HashSet<BlockLocation>())); | |
getServer().getPluginManager().registerEvents(this, this); | |
} | |
@EventHandler(priority = EventPriority.MONITOR) | |
public void onPlayerDeath(final PlayerDeathEvent event) { | |
if (event.getEntity().getKiller() != null) | |
signsLoop: for (final BlockLocation location : signs) { | |
final Block block = location.getBlock(); | |
if (block.getState() != null && block.getState() instanceof Sign) { | |
final Sign sign = (Sign) block.getState(); | |
for (int i = 0; i < 4; i++) | |
if (StringUtils.isBlank(sign.getLine(i))) { | |
sign.setLine(i, event.getEntity().getKiller().getName()); | |
sign.update(true); | |
break signsLoop; | |
} | |
} | |
} | |
} | |
private void saveSigns() { | |
final YamlConfiguration config = YamlConfiguration.loadConfiguration(INTERNAL); | |
config.set("signs", signs); | |
try { | |
config.save(INTERNAL); | |
} catch (final IOException e) {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment