[Solved]im having troubles with my code

Discussion in 'Plugin Development' started by kman2010, May 3, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    kman2010

    Last edited by a moderator: May 14, 2016
  2. Offline

    mindless728

    1) put the code in code tags so it is easier to read (honestly, is it easy to read like that?)
    2) get rid of the bold tags (as they will show up in the code tags as tags)
    3) wait longer than 4 minutes before bumping
     
  3. Offline

    kman2010

    there

    1. BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  4. Offline

    Moon_werewolf

    i think the problem is in your TDBlockListener.java for you have the log after the if statement

    try replace your onBlockBreak with this
    Code:
    public void onBlockBreak(BlockBreakEvent event)
        {
            Block block = event.getBlock();
            Player player = event.getPlayer();
    
            if(block.getType() == Material.TORCH && plugin.enabled(player))
            {
                player.sendMessage(ChatColor.BLUE + "You broke a torch.");
                log.info(player.getName() + " BROKE A TORCH");
            }
        }
     
  5. Offline

    kman2010

    thnx i got it now but now i need to know how to auto ban the player when he or she places that block.

    also it needs to say to ingame ops the place where the block was broke

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  6. Offline

    neromir

    I understand you want help, but you're going about it the wrong way.

    First, don't bump it so much, it makes you look impatient and unwilling to try to solve the problem yourself. Secondly, format your code using code tags as was requested by mindless728. If you can't do that, dump it into pastebin and put the link here, it'd be much easier to read.

    Lastly, specifically PMing random devs to come post what the solution is on your thread is in bad taste; especially if you haven't taken the suggestions given by previous devs.
     
  7. Offline

    kman2010

    Last edited by a moderator: May 14, 2016
  8. Offline

    neromir

    On a side note, you can set the syntax highlighting in PasteBin to Java so it will make it prettier for you; something that would be good to do for next time.

    At any rate, in the short amount of time I have available to search, I was not able to find an outright ban player method. I recommend looking around in here: http://javadoc.lukegb.com/BukkitAPI/ Try searching for various terms that seem likely; "ban" didn't seem to turn up anything useful immediately.

    Alternatively, the hack-ish idea I came up with on the fly is something like

    Code:
    public void onBlockBreak(BlockBreakEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            
            if(block.getType() == Material.TNT && plugin.enabled(player)){
                player.sendMessage(ChatColor.RED + "You broke TNT you could get banned.");
                log.info("(TD)" + "[" + player.getName() + "]" + " BROKE TNT");
    
                plugin.getServer().dispatchCommand(<some valid command sender, like Console>, "ban " + player.getName());
    
            }
        }
    
    You'd need to look up how to create a valid CommandSender on the fly, though, and I'm not at all sure this would work.
     
  9. Offline

    kman2010

    thank you very much

    thank you so much it worked


    http://pastebin.com/5SwCX5A5
    http://pastebin.com/PP6brcGr
    http://pastebin.com/BsW9UhTf
    http://pastebin.com/kBLFBYQw
    that is my full source as of now i update it when i can but i need some help i need to know how to inside of TD.java how do i make the command /td able to direct to a player like /td (player name) if you can please give me the whole TD.java if not that then just the parts you edit i need this soon.
    thank you,
    kman2010.​

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  10. Offline

    Acrobot

  11. Offline

    kman2010

    thank you i think this will work idk yet brb

    it works thank you so much

    there i have up;dated the code and the question

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  12. Offline

    Unscrewed

    To kick, try something like that:

    Code:
    player.kickPlayer(player.getName() + ", you are kicked for breaking TNT.");
    
     
  13. Offline

    kman2010

    i know but i need it to make a config.yml file that you can edit and chose one of these choises kick,ban,or nothing happens but tells the server that person placed tnt

    i still need help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  14. Offline

    Daniel Heppner

    Look at someone else's plugin. This is an easy thing to do.
     
  15. Offline

    kman2010

    i know i have but i dont get it at all i want and for some reason need someone to right a snipit of code with comments in it so i can understand it.

    oh and im also reqruting people for a big pugin if anyone is interested just pm me.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  16. Offline

    Unscrewed

    You need to load the config.yml at start, then you could add variables, like:
    ban, kick etc...
    Then do something like:
    Code:
    if(config.kick){
    player.kickPlayer(player.getName() + ", you are kicked for breaking TNT.");
    }
    
    That's obviously not the code but that is a way to do it.
     
  17. Offline

    kman2010

    but how do i make it make and load the configuration file
     
  18. Offline

    Unscrewed

    Check out some other simple plugins with config files.
    If they dont have the source up, decompile it with DJ Java Decompiler 3.11
     
  19. Offline

    kman2010

    ok

    updated my post

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  20. Offline

    Spitfire21

  21. Offline

    kman2010

    that is not going to help it is to compicated i need to see a plugin that only has 1 option in the config.yml
     
  22. Offline

    Spitfire21

    Set it like that
    Code:
    config.load();
    config.setProperty("Ban", false);
    config.save();
    
    Then you can load from it later by using
    if(config.Ban == false){
    // Do Stuff
    }
     
  23. Offline

    kman2010

    ok thnx im going to test it in my code

    ok now it is giving me this error in eclipse
    Code:
    package me.kman2010.TD;
    import java.io.File;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.util.config.Configuration;
    public class TDBlockListener extends BlockListener {
        public static TD plugin;
        private static final Logger log = Logger.getLogger("Minecraft");
        Configuration config = new Configuration(new File("plugins/TD", "config.yml"));
        public TDBlockListener(TD instance) {
            plugin = instance;
        }
        public void onBlockPlace(BlockPlaceEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            config.load();
            config.setProperty("Ban", false);
            config.save();
            if(config.Ban == false){
            if(block.getType() == Material.TNT && plugin.enabled(player)){
                player.sendMessage(ChatColor.RED + "You broke TNT you could get banned.");
                log.info("(TD)" + "[" + player.getName() + "]" + " BROKE TNT");
                plugin.getServer().dispatchCommand( null, "ban " + player.getName());
            }
        }
    }
    }
    
    there is an error with this line "if(config.Ban == false){"
    if you can help me fix it i will give you a cookie :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  24. Offline

    Spitfire21

    Remove the config lines from the TDBlockListener.
    Place this in onEnable in your TD file
    Code:
    Configuration config = new Configuration(new File("plugins/Sprint", "config.yml"));
    config.load();
    config.setProperty("Ban", false);
    config.save();
    Banplayer = config.GetBoolean("Ban",false);
    Then in your TDBlockListener onBlockPlace function do if(plugin.Banplayer == false){
    //do stuff
    }

    
     
  25. Offline

    kman2010

    spit fire it did not work
     
  26. Offline

    Spitfire21

    Its cause it was placed in the wrong file and I forgot to post the boolean variable; just remove this from your file and add it to the onEnable function in TD file:
    Configuration config = new Configuration(new File("plugins/Sprint", "config.yml"));
    config.load();
    config.setProperty("Ban", false);
    config.save();
    Then add this to TD after you added those lines:
    Banplayer = config.GetBoolean("Ban",false);
    Then on your TDBlockListener onBlockPlace function add the confitional check
    if(plugin.Banplayer == false){
    //dostuff
    }
     
  27. Offline

    kman2010

    OK THANKS

    i got it i think

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 14, 2016
  28. Offline

    Spitfire21

    Sounds good make sure you put a check so it doesn't keep rewriting the file.
    if (!new File("YOURDIRECTORY").exists()){
    // Config stuff
    }
    else{
    //Read stuff
    }
    Hope it works:)
     
  29. Offline

    kman2010

    package me.kman2010.TD;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.logging.Logger;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.Event.Priority;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
    import org.bukkit.util.config.ConfigurationNode;

    public class TD extends JavaPlugin {

    private static final Logger log = Logger.getLogger("Minecraft");
    private final TDBlockListener blockListener = new TDBlockListener(this);
    private final TDEntityListener entityListener = new TDEntityListener(this);
    private final TDPlayerListener playerListener = new TDPlayerListener(this);
    public final HashMap<Player, ArrayList<Block>> tdUsers = new HashMap<Player, ArrayList<Block>>();
    public boolean Banplayer;
    private ConfigurationNode config;

    public void onEnable() {
    if (!new File("YOURDIRECTORY").exists()){
    Configuration config = new Configuration(new File("plugins/TD", "config.yml"));
    config.load();
    config.setProperty("Ban", false);
    config.save();}
    Banplayer = config.getBoolean("Ban",false);
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener,
    Event.Priority.Normal, this);
    pm.registerEvent(Event.Type.EXPLOSION_PRIME, entityListener, Priority.Lowest, this);
    pm.registerEvent(Event.Type.PLAYER_LOGIN, playerListener,
    Event.Priority.Normal, this);

    log.info("TD 1.4 ENABLED");
    }

    public void onDisable() {
    log.info("TD 1.4 DISABLED");
    }
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    if(commandLabel.equalsIgnoreCase("TD")){

    if(sender.isOp()) {
    Player sendPlayer = (Player) sender; //We assign sendPlayer to out command sender
    if(args.length >= 1){ //We check if we have got any argument
    Player player = getServer().getPlayer(args[0]); //We try to get the player from the argument
    if(player == null){
    sendPlayer.sendMessage("Oops, that player doesn't exist!"); //Ooops, we have no player!
    }else{
    toggleTD(player); //We toggle the TD for our player from argument
    }

    }else{
    toggleTD(sendPlayer); //We haven't got any arguments, let's do this on ourselves!
    }

    return true;
    }
    return false;}
    return false;
    }
    public void toggleTD(Player player){
    if(enabled(player)){
    this.tdUsers.remove(player);

    if(player.isOnline()) {
    player.sendMessage("TD Disabled");
    }
    }else{
    this.tdUsers.put(player, null);

    if(player.isOnline()) {
    player.sendMessage("TD Enabled");
    }
    }
    }
    public boolean enabled(Player player){
    return this.tdUsers.containsKey(player);
    }
    }
    _________________________________________________________________
    am i doing it wrong because it keeps over wrighting the files
     
  30. Offline

    Archelaus

    Use pastebin or code tags.
     
Thread Status:
Not open for further replies.

Share This Page