Skip to content

Instantly share code, notes, and snippets.

@Alvin-LB
Last active February 11, 2017 13:35
Show Gist options
  • Save Alvin-LB/a6116c8ab849a12e59b8561814ee0beb to your computer and use it in GitHub Desktop.
Save Alvin-LB/a6116c8ab849a12e59b8561814ee0beb to your computer and use it in GitHub Desktop.
name: TNTStep
version: 1.0-SNAPSHOT
main: com.bringholm.tntstep.TNTStep
author: AlvinB
commands:
tntstep:
description: |
/tntstep on [world] - Turns on TNTStep in the world the player is in, or the world optionally specified world in the third argument
/tntstep off [world] - Turns off TNTStep in the world the player is in, or the world optionally specified world in the third argument
/tntstep reload - Reloads the config for TNTStep
/tntstep list - Lists all worlds TNTStep is enabled in.
usage: /tntstep <on|off|reload|list> [world]
permissions:
tntstep.*:
description: Gives access to all permissions of this plugin.
children:
tntstep.toggle: true
tntstep.reload: true
tntstep.toggle:
description: Allows you to toggle TNTStep on and off in a certain world
default: op
tntstep.reload:
description: Allows you to reload the TNTStep config
default: op
tntstep.list:
description: Allows you to list all the worlds TNTStep is enabled in
default: op
package com.bringholm.tntstep;
import com.google.common.collect.Lists;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
public class TNTStep extends JavaPlugin implements Listener {
private List<String> activeWorlds = Lists.newArrayList();
@Override
public void onEnable() {
this.getServer().getPluginManager().registerEvents(this, this);
reloadConfigValues();
}
@Override
public void onDisable() {
saveConfigValues();
}
private void saveConfigValues() {
this.getConfig().set("active-worlds", activeWorlds);
this.saveConfig();
}
private boolean reloadConfigValues() {
try {
saveDefaultConfig();
if (getConfig().contains("active-worlds")) {
activeWorlds = getConfig().getStringList("active-worlds");
} else {
this.getLogger().warning("config.yml does not contain the list of active worlds!");
return false;
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@EventHandler
public void onMove(PlayerMoveEvent e) {
if (activeWorlds.contains(e.getTo().getWorld().getName())) {
Block block = e.getTo().getBlock().getRelative(BlockFace.DOWN);
if (block.getType() == Material.TNT && e.getPlayer().isOnGround()) {
block.setType(Material.AIR);
block.getWorld().spawnEntity(block.getLocation().add(0.5, 0, 0.5), EntityType.PRIMED_TNT);
}
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0 || args.length > 2) {
sender.sendMessage(ChatColor.RED + "Incorrect arguments!");
return true;
}
switch (args[0]) {
case "on":
if (!sender.hasPermission("tntstep.toggle")) {
sender.sendMessage(ChatColor.RED + "You don't have permission to do this!");
return true;
}
if (args.length == 2) {
if (Bukkit.getWorld(args[1]) != null) {
if (activeWorlds.contains(args[1])) {
sender.sendMessage(ChatColor.RED + "TNTStep is already active in this world!");
} else {
activeWorlds.add(args[1]);
saveConfigValues();
sender.sendMessage(ChatColor.GOLD + "TNTStep is now active in world " + args[1] + "!");
}
} else {
sender.sendMessage(ChatColor.RED + "World " + args[1] + "could not be found!");
}
} else if (sender instanceof Player) {
String worldName = ((Player) sender).getWorld().getName();
if (activeWorlds.contains(worldName)) {
sender.sendMessage(ChatColor.RED + "TNTStep is already active in this world!");
} else {
activeWorlds.add(worldName);
saveConfigValues();
sender.sendMessage(ChatColor.GOLD + "TNTStep is now active in world " + worldName + "!");
}
} else {
sender.sendMessage(ChatColor.RED + "You must specify a world to activate TNTStep in!");
}
break;
case "off":
if (!sender.hasPermission("tntstep.toggle")) {
sender.sendMessage(ChatColor.RED + "You don't have permission to do this!");
return true;
}
if (args.length == 2) {
if (activeWorlds.contains(args[1])) {
activeWorlds.remove(args[1]);
saveConfigValues();
sender.sendMessage(ChatColor.GOLD + "TNTStep is no longer active in world " + args[1] + "!");
} else {
sender.sendMessage(ChatColor.RED + "TNTStep is not active in this world!");
}
} else if (sender instanceof Player) {
String worldName = ((Player) sender).getWorld().getName();
if (activeWorlds.contains(worldName)) {
activeWorlds.remove(worldName);
saveConfigValues();
sender.sendMessage(ChatColor.GOLD + "TNTStep is no longer active in world " + worldName + "!");
} else {
sender.sendMessage(ChatColor.RED + "TNTStep is not active in this world!");
}
} else {
sender.sendMessage(ChatColor.RED + "You must specify a world to deactivate TNTStep in!");
}
break;
case "reload":
if (!sender.hasPermission("tntstep.reload")) {
sender.sendMessage(ChatColor.RED + "You don't have permission to do this!");
}
if (reloadConfigValues()) {
sender.sendMessage(ChatColor.GOLD + "Successfully reloaded config!");
} else {
sender.sendMessage(ChatColor.RED + "Something went wrong while reloading the config! Check the console for details!");
}
break;
case "list":
if (!sender.hasPermission("tntstep.list")) {
sender.sendMessage(ChatColor.RED + "You don't have permission to do this!");
return true;
}
if (activeWorlds.size() == 0) {
sender.sendMessage(ChatColor.GOLD + "There are no worlds to display!");
} else {
sender.sendMessage(ChatColor.GOLD + "Listing all worlds TNTStep is enabled in:");
for (String string : activeWorlds) {
sender.sendMessage(ChatColor.GOLD + string);
}
}
break;
default:
sender.sendMessage(ChatColor.RED + "Incorrect arguments!");
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment