[Java] Block Denying

Any help topics that don't fit in the current categories can go here.
Post Reply
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

[Java] Block Denying

Post 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
[syntax=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(){

}
}
[/syntax]

NoMoeTNTTNTlistener.java
[syntax=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);
}
}
}
[/syntax]

plugin.yml
[syntax=java]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>[/syntax]

NoMoeTNTCommandExecutor.java
[syntax=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;
}
}[/syntax]

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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: [Java] Block Denying

Post 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

[syntax=java]if (player.hasPermission("something.example.thing")){
// the player has that permission.
}[/syntax]
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
Image
Z645
Posts: 33
Joined: Thu Jul 26, 2012 5:08 pm

Re: [Java] Block Denying

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: [Java] Block Denying

Post by jacek »

To block more than one type of block I would try to make it a bit more dynamic by using a list.

[syntax=java]ArrayList<Material> blocked = new ArrayList<Material>();
blocked.add(Material.TNT);
blocked.add(Material.LAVA);
blocked.add(Material.BEDROCK);[/syntax]

then in the event handler you can check if the block is one of these

[syntax=java]if (blocked.contains(event.getBlock().getType())){
// bad block.
}[/syntax]

and you can use the Material to send the message dynamically.

[syntax=java]Material type = event.getBlock().getType();

if (blocked.contains(type)){
player.sendMessage("You cannot place " + type.name() + "!");
event.setCancelled(true);
}[/syntax]

Hope that helps :D
Image
Post Reply