Created
January 18, 2017 14:22
-
-
Save finreinhard/6af23617c35b3bd05b80612ccc57baf7 to your computer and use it in GitHub Desktop.
BlockedList
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 li.angu.youtube.tutorial.block; | |
import org.bukkit.Material; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by finreinhard on 18.01.17. | |
*/ | |
public class BlockedList { | |
private static BlockedList instance; | |
private List<Material> blocks; | |
private List<String> commands; | |
private BlockedList() { | |
instance = this; | |
this.blocks = new ArrayList<>(); | |
this.commands = new ArrayList<>(); | |
fillWithTestData(); | |
} | |
public static BlockedList getInstance() { | |
return instance == null ? new BlockedList() : instance; | |
} | |
public void fillWithTestData() { | |
blocks.add(Material.TNT); | |
blocks.add(Material.GRASS); | |
blocks.add(Material.BEDROCK); | |
commands.add("op"); | |
commands.add("deop"); | |
} | |
public boolean isBlockBlocked(Material material) { | |
return blocks.contains(material); | |
} | |
public boolean isCommandBlocked(String command) { | |
// /op Firei | |
/* | |
Java 6+7 | |
for(String blockedCommand : this.commands) { | |
if(command.toLowerCase().startsWith("/" + blockedCommand)) { | |
return true; | |
} | |
} | |
return false; | |
*/ | |
// Java 8 | |
return this.commands.stream().filter( | |
blockedCommand -> command.toLowerCase().startsWith("/" + blockedCommand)) | |
.findFirst().orElse(null) != null; | |
} | |
public void addBlock(Material material) { | |
this.blocks.add(material); | |
} | |
public void addCommand(String command) { | |
this.commands.add(command); | |
} | |
public void removeBlock(Material material) { | |
if (!this.blocks.contains(material)) { | |
return; | |
} | |
this.blocks.remove(material); | |
} | |
public void removeCommand(String command) { | |
if (!this.commands.contains(command)) { | |
return; | |
} | |
this.commands.remove(command); | |
} | |
} |
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 li.angu.youtube.tutorial.block; | |
import net.md_5.bungee.api.ChatColor; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.block.BlockPlaceEvent; | |
import org.bukkit.event.player.PlayerCommandPreprocessEvent; | |
/** | |
* Created by finreinhard on 18.01.17. | |
*/ | |
public class BlockedListListener implements Listener { | |
@EventHandler | |
public void onBlockPlace(BlockPlaceEvent event) { | |
if (BlockedList.getInstance().isBlockBlocked(event.getBlock().getType())) { | |
event.setCancelled(true); | |
event.getPlayer().sendMessage(ChatColor.RED + "Diesen Block darfst du nicht platzieren!"); | |
} | |
} | |
@EventHandler | |
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { | |
if (BlockedList.getInstance().isCommandBlocked(event.getMessage())) { | |
event.setCancelled(true); | |
event.getPlayer().sendMessage(ChatColor.RED + "Diesen Befehl darfst du nicht ausführen!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment