Skip to content

Instantly share code, notes, and snippets.

@NeatMonster
Created March 7, 2012 20:01
Show Gist options
  • Save NeatMonster/1995657 to your computer and use it in GitHub Desktop.
Save NeatMonster/1995657 to your computer and use it in GitHub Desktop.
Checkpoint
package fr.alaric1001.checkpoint;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.Sign;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
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.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Checkpoint extends JavaPlugin implements Listener {
private final Map<String, Location> checkpoints = new HashMap<String, Location>();
@Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
if (command.getName().equalsIgnoreCase("checkpoint") && args.length == 1
&& args[0].equalsIgnoreCase("remove") && sender instanceof Player) {
checkpoints.remove(sender.getName());
sender.sendMessage(ChatColor.GREEN + "Checkpoint removed!");
return true;
}
return false;
}
@Override
public void onEnable() {
super.onEnable();
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(final PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Sign
&& (event.getPlayer().isOp() || event.getPlayer().hasPermission("checkpoint.use"))) {
final Sign sign = (Sign) event.getClickedBlock().getState();
if (sign.getLine(0).equals(ChatColor.BLUE + "[CheckPoint]")
&& sign.getLine(1).equals(ChatColor.BLACK + "Right-click")
&& sign.getLine(2).equals(ChatColor.BLACK + "to set your")
&& sign.getLine(3).equals(ChatColor.BLACK + "checkpoint!")) {
checkpoints.put(event.getPlayer().getName(), event.getPlayer().getLocation());
event.getPlayer().sendMessage(ChatColor.GREEN + "Checkpoint defined! Use /checkpoint remove to remove it.");
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerRespawn(final PlayerRespawnEvent event) {
if (checkpoints.containsKey(event.getPlayer().getName()))
event.setRespawnLocation(checkpoints.get(event.getPlayer().getName()));
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onSignChange(final SignChangeEvent event) {
if (event.getLine(0).equalsIgnoreCase("[CheckPoint]"))
if (event.getPlayer().isOp() || event.getPlayer().hasPermission("checkpoint.create")) {
event.setLine(0, ChatColor.BLUE + "[CheckPoint]");
event.setLine(1, ChatColor.BLACK + "Right-click");
event.setLine(2, ChatColor.BLACK + "to set your");
event.setLine(3, ChatColor.BLACK + "checkpoint!");
event.getPlayer().sendMessage(ChatColor.GREEN + "Checkpoint successfully created!");
} else {
event.setLine(0, ChatColor.RED + "[CheckPoint]");
event.setLine(1, ChatColor.BLACK + "");
event.setLine(2, ChatColor.BLACK + "");
event.setLine(3, ChatColor.BLACK + "");
event.getPlayer().sendMessage(ChatColor.RED + "You're not allowed to do that!");
}
}
}
name: Checkpoint
main: fr.alaric1001.checkpoint.Checkpoint
author: alaric1001
version: 1.2
commands:
checkpoint:
description: Main command of Checkpoint.
aliases: [cp, ckp, chkp, checkp]
usage: Only one command ATM, /<command> return.
@Saungnc
Copy link

Saungnc commented Jul 28, 2016

Can I make checkpoint only these two files? I have found many errors when I run these two files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment