Page 1 of 1

[Java] Block Denying

Posted: Wed Nov 14, 2012 10:41 pm
by Z645
So I got the TNTNotifier code. And I wanted to add more stuff onto it like Permissions and Commands. But I'm not getting how that would happen. I've worked a bit on the Commands and Permissions but suddenly the plugin stopped working. So heres the code:

NoMoeTNT.java
package me.Z645.nomoetnt;

import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class NoMoeTNT extends JavaPlugin {
	
	public void onEnable(){
		PluginManager manager = this.getServer().getPluginManager();
		
		manager.registerEvents(new NoMoeTNTTNTListener(), this);
		
		this.getCommand("nomoetnt").setExecutor(new NoMoeTNTCommandExecutor(this));
	}
	
	public void onDisable(){
		
	}
}
NoMoeTNTTNTlistener.java
package me.Z645.nomoetnt;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;

public class NoMoeTNTTNTListener implements Listener {
	
	@EventHandler(priority = EventPriority.HIGH)
	public void onBlockPlace(BlockPlaceEvent event){
		if (event.isCancelled()) return;
		
		if (event.getBlock().getType() == Material.TNT){
			event.setCancelled(true);
			
			Player player = event.getPlayer();
			
			player.sendMessage(ChatColor.RED + "You are not allowed to place TNT!");
			
			for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()){
				if (onlinePlayer.isOp()){
					onlinePlayer.sendMessage(ChatColor.RED + "Player " + player.getName() + " tried placing a block of TNT!");
				}
			}
		}
		
		if (event.getBlock().getType() == Material.LAVA){
			event.setCancelled(true);
			
			Player player = event.getPlayer();
			
			player.sendMessage(ChatColor.RED + "You are not allowed to place Lava!");
			
			for (Player onlinePlayer : Bukkit.getServer().getOnlinePlayers()){
				if (onlinePlayer.isOp()){
					onlinePlayer.sendMessage(ChatColor.RED + "Player" + player.getName() + "tried to place Lava");
				}
			}
		}
	}
	
	@EventHandler(priority = EventPriority.HIGH)
	public void onExplosionPrime(ExplosionPrimeEvent event){
		if (event.isCancelled()) return;
			
		if (event.getEntity() instanceof TNTPrimed){
			event.setCancelled(true);
		}
	}
}
plugin.yml
name: NoMoeTNT
version: 0.2
main: me.Z645.nomoetnt.NoMoeTNT

commands:
   nomoetnt:
      description: Enables and Disables the plugin.
      usage: /nomoetnt
      permission: nomoetnt.basic
      permission-message: You don't have <permission>
NoMoeTNTCommandExecutor.java
package me.Z645.nomoetnt;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class NoMoeTNTCommandExecutor implements CommandExecutor{
	
	public NoMoeTNTCommandExecutor(NoMoeTNT plugin){
		
	}
	
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
		onLoad();
		return false;
	}
}
Also, if anyone could help me: I am trying to add more blocks to be denied, but I don't know if I put it in the first Public Class, or do I make a new one for it.

Re: [Java] Block Denying

Posted: Sat Nov 17, 2012 12:12 am
by jacek
Well before you just "add commands" you should think about what those commands should be doing, in this case I can't think of any sensible use for a command in a plugin like this. For permissions you can check them using the player object like this
if (player.hasPermission("something.example.thing")){
    // the player has that permission.
}
Other than that, what is wrong with the plugin as it is now ? If you tell use the error we might be able to help :D

Re: [Java] Block Denying

Posted: Sat Nov 17, 2012 3:53 am
by Z645
I'll try that permissions. And for commands, I was thinking of maybe something like an on/off command?

Also, for adding more blocks to be denied. Where to I put it? Cause I've put them together, and it only shows the Messages for the first block denied only. It doesn't even deny placing of that block.

Re: [Java] Block Denying

Posted: Tue Nov 20, 2012 12:13 am
by jacek
To block more than one type of block I would try to make it a bit more dynamic by using a list.
ArrayList<Material> blocked = new ArrayList<Material>();
blocked.add(Material.TNT);
blocked.add(Material.LAVA);
blocked.add(Material.BEDROCK);
then in the event handler you can check if the block is one of these
if (blocked.contains(event.getBlock().getType())){
    // bad block.
}
and you can use the Material to send the message dynamically.
Material type = event.getBlock().getType();

if (blocked.contains(type)){
    player.sendMessage("You cannot place " + type.name() + "!");
    event.setCancelled(true);
}
Hope that helps :D