Replacing text

Discussion in 'Plugin Development' started by WeDaBeast, Jun 7, 2014.

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

    WeDaBeast

    Code:java
    1. package me.mineactivity;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.AsyncPlayerChatEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener{
    16.  
    17. @Override
    18. public void onDisable() {
    19.  
    20. }
    21.  
    22. @Override
    23. public void onEnable() {
    24. this.getServer().getPluginManager().registerEvents(new Main(), this);
    25. }
    26.  
    27.  
    28.  
    29. public static HashMap<Player, Boolean> dolan = new HashMap<Player, Boolean>();
    30.  
    31. public void togglePluginState(Player player){
    32.  
    33. if(dolan.containsKey(player)){
    34. if(dolan.get(player)){
    35. dolan.put(player, false);
    36. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "X" + ChatColor.DARK_GRAY + "]" + " Dolan mode has been disabled.");
    37. } else {
    38. dolan.put(player, true);
    39. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "X" + ChatColor.DARK_GRAY + "]" + " Dolan mode has been enabled.");
    40.  
    41. }
    42. } else {
    43. dolan.put(player, true); //If you want plugin enabled by default change this value to false.
    44. player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.YELLOW + "X" + ChatColor.DARK_GRAY + "]" + " Dolan mode has been enabled.");
    45. }
    46.  
    47. }
    48.  
    49. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    50. if(cmd.getName().equalsIgnoreCase("dolan")){
    51. Player player = (Player) sender;
    52. togglePluginState(player);
    53. return true;
    54. }
    55. return false;
    56. }
    57.  
    58. @EventHandler
    59. public void Chat(AsyncPlayerChatEvent event) {
    60. if (dolan.containsKey(event.getPlayer())){
    61. if (dolan.containsValue(true)){
    62. String message = event.getMessage()
    63. .replaceAll("what", "wot")
    64. .replaceAll("uncle", "unkle")
    65. .replaceAll("mom", "mum")
    66. .replaceAll("dad", "ded")
    67. .replaceAll("mother", "mowthar")
    68. .replaceAll("father", "futhar")
    69. .replaceAll("how", "hew")
    70. .replaceAll("pizza", "puzzei")
    71. .replaceAll("I", "dolen")
    72. .replaceAll("myself", "dolen");
    73.  
    74.  
    75.  
    76. event.setMessage(message);
    77. }
    78.  
    79.  
    80. }
    81. }
    82. }
    83.  
    84.  
    85.  
     
  2. Offline

    teej107

    Yes I can see that you are replacing text.
     
  3. Offline

    WeDaBeast

    teej107 Lmao sorry about that, it doesn't seem to be working, like the command activates, but the chat doesn't get replaced for some reason

    Someone mind taking a look at this?

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

    mine-care

    Think you have to use replace not replace all...
     
  5. Offline

    WeDaBeast

    mine-care Doesn't seem to work either... Hmm strange
     
  6. Offline

    Code0

    WeDaBeast You got some coding errors.

    Code:java
    1. if(dolan.get(player))
    2.  
    3. should be:
    4.  
    5. if(dolan.get(player) == <someBooleanHere>)


    And:

    Code:
    if (dolan.containsValue(true))
     
    should be:
     
    if(dolan.get(event.getPlayer()) == <someBooleanValue>)
    Okay?

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

    WeDaBeast

    Code0 The toggle its self isnt the issue the "replacing" part is..
     
  8. Offline

    WhatAaCow

    WeDaBeast no it's not. You used the wrong statements like Code0 said.
     
  9. Offline

    Code0

    WeDaBeast Your if(dolan.containsValue(true){
    will always return true/false... try changing it to: if(dolan.get(event.getPlayer()) == true)
     
  10. Offline

    WeDaBeast

    Code0 Still not working for some odd reason
     
  11. Offline

    mine-care

    WeDaBeast when I go home (few hours) ill work on your code and make it work, it's good to use debug code to see if and where code execution stops.

    Btw on register event try instead of new main , to use this (if the main class is the class where you have both event listeners and onenable or ondisable

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

    theguynextdoor

    Some things I see.

    1. You are using static for your dolan map ... you don't need it to be static therefore it should not be static
    2. You are using a Player, Boolean for your map. This is superfluous, just use a list.
    3. Ignore what code0 said about boolean comparisons in that you do not need the == when doing booleans in the if statement. Doing if (!boolean) or if (boolean) is preferred over if (boolean == false) etc.
    4. It would be more suitable to store a player's UUID since you clearly are not handling the player object correctly.
    5. In your command you cast the sender to a player without checking it is a player first. This is bad, because what if I did this command in the console? You would get a nice error, that's what.

    But the main fix would be to remove the hashmap and make it a list. .... and remove the static.
    (and as mine-care said, when registering your events, dont use 'new Main()' use 'this')
     
Thread Status:
Not open for further replies.

Share This Page