NMS Custom Command sender

Discussion in 'Resources' started by The_Spaceman, Sep 9, 2018.

?

Is this usefull?

  1. yes

    1 vote(s)
    50.0%
  2. no

    1 vote(s)
    50.0%
  1. Offline

    The_Spaceman

    I have created a class that is a custom command sender
    You can use this to use NMS commands in your own plugins

    Show Spoiler
    Code:java
    1. import net.minecraft.server.v1_13_R2.*;
    2. import org.bukkit.command.CommandSender;
    3. import org.bukkit.craftbukkit.v1_13_R2.CraftServer;
    4. import org.bukkit.craftbukkit.v1_13_R2.CraftWorld;
    5. import org.bukkit.craftbukkit.v1_13_R2.entity.CraftMinecartCommand;
    6. import org.bukkit.craftbukkit.v1_13_R2.entity.CraftPlayer;
    7. import org.bukkit.entity.Player;
    8.  
    9. import javax.annotation.Nullable;
    10.  
    11. public class CustomCommandSender extends CraftMinecartCommand {
    12.  
    13. private Player player;
    14.  
    15. public CustomCommandSender(Player player) {
    16. super((CraftServer) (player).getServer(), new CustomMinecart(((CraftWorld) player.getWorld()).getHandle()));
    17. this.player = player;
    18. ((CustomMinecart) this.getHandle()).setCustomCommandListenerWrapper((CustomCommandListenerWrapper) getCommandListenerWrapper());
    19. }
    20.  
    21. private CommandListenerWrapper getCommandListenerWrapper() {
    22. EntityPlayer p = ((CraftPlayer) player).getHandle();
    23.  
    24. return new CustomCommandListenerWrapper(new CustomCommandListener(p, this), p.bI(), p.aO(), p.getWorldServer(), 0, "", null, p.server, null);
    25. }
    26.  
    27. @Override
    28. public void sendMessage(String message) {
    29. System.out.println("Bukkit command: " + message);
    30. }
    31.  
    32. private static class CustomCommandListener implements ICommandListener {
    33. EntityPlayer player;
    34. CustomCommandSender customCommandSender;
    35.  
    36. public CustomCommandListener(EntityPlayer player, CustomCommandSender customCommandSender) {
    37. this.player = player;
    38. this.customCommandSender = customCommandSender;
    39. }
    40.  
    41.  
    42. @Override
    43. public void sendMessage(IChatBaseComponent iChatBaseComponent) {
    44. }
    45.  
    46. @Override
    47. public boolean a() {
    48. return player.a();
    49. }
    50.  
    51. @Override
    52. public boolean b() {
    53. return player.b();
    54. }
    55.  
    56. @Override
    57. public boolean B_() {
    58. return player.B_();
    59. }
    60.  
    61. @Override
    62. public CommandSender getBukkitSender(CommandListenerWrapper commandListenerWrapper) {
    63. return customCommandSender;
    64. }
    65. }
    66.  
    67. private static class CustomMinecart extends EntityMinecartCommandBlock {
    68. private CustomCommandListenerWrapper customCommandListenerWrapper;
    69. private CustomCommandBlock customCommandBlock;
    70.  
    71. public CustomMinecart(net.minecraft.server.v1_13_R2.World world) {
    72. super(world);
    73. }
    74.  
    75. @Override
    76. public CommandListenerWrapper getCommandListener() {
    77. return customCommandListenerWrapper;
    78. }
    79.  
    80. public void setCustomCommandListenerWrapper(CustomCommandListenerWrapper customCommandListenerWrapper) {
    81. this.customCommandListenerWrapper = customCommandListenerWrapper;
    82. this.customCommandBlock = new CustomCommandBlock(customCommandListenerWrapper);
    83. }
    84.  
    85. @Override
    86. public CommandBlockListenerAbstract getCommandBlock() {
    87. return customCommandBlock;
    88. }
    89. }
    90.  
    91. private static class CustomCommandBlock extends CommandBlockListenerAbstract {
    92. CustomCommandListenerWrapper customCommandListenerWrapper;
    93.  
    94. public CustomCommandBlock(CustomCommandListenerWrapper customCommandListenerWrapper) {
    95. this.customCommandListenerWrapper = customCommandListenerWrapper;
    96. }
    97.  
    98. @Override
    99. public CommandSender getBukkitSender(CommandListenerWrapper commandListenerWrapper) {
    100. return null;
    101. }
    102.  
    103. @Override
    104. public WorldServer d() {
    105. return null;
    106. }
    107.  
    108. @Override
    109. public void e() {
    110.  
    111. }
    112.  
    113. @Override
    114. public CommandListenerWrapper getWrapper() {
    115. return customCommandListenerWrapper;
    116. }
    117. }
    118.  
    119. private class CustomCommandListenerWrapper extends CommandListenerWrapper {
    120. public CustomCommandListenerWrapper(ICommandListener icommandlistener, Vec3D vec3d, Vec2F vec2f, WorldServer worldserver, int i,
    121. String s, IChatBaseComponent ichatbasecomponent, MinecraftServer minecraftserver, @Nullable Entity entity) {
    122. super(icommandlistener, vec3d, vec2f, worldserver, i, s, ichatbasecomponent, minecraftserver, entity);
    123. }
    124.  
    125. @Override
    126. public void sendFailureMessage(IChatBaseComponent ichatbasecomponent) {
    127. System.out.println("failed vanilla command: " + ichatbasecomponent.getString());
    128. }
    129.  
    130. @Override
    131. public void sendMessage(IChatBaseComponent ichatbasecomponent, boolean flag) {
    132. System.out.println("vanilla command: " + ichatbasecomponent.getString());
    133. }
    134. }
    135. }


    usage:
    Code:java
    1. Bukkit.dispatchCommand(new CustomCommandSender(player), "locate Monument");
     
  2. Online

    timtower Ninja on the waves Moderator

    @The_Spaceman Why the System.out.println and not using a logger?
    But what does it do? What does it differently from using a Player as CommandSender ?
     
  3. Offline

    The_Spaceman

    System.out.println is shorter in Intellij to type (type 'sout' and that will appear...)

    when using a player as CommandSender the player will recieve all the messages, but when you want to get the location of a Monument (for example) you can't use Bukkit.findMonument or someting. so you use this... and use the IChatBaseComponent to extract the location of the nearest Monument.
    This is for getting the return values of commands (for Minecraft commands of Bukkit commands)
     
  4. Online

    timtower Ninja on the waves Moderator

    @The_Spaceman How about a custom log function? And maybe an interface so you can get the values out of the commands being used.
     
  5. Offline

    The_Spaceman

    That is possible, but I'm not going to make that, because it all depends on what command is used. and it would be a hell to make this possible for all commands in the whole Bukkit comunity...
     
  6. Online

    timtower Ninja on the waves Moderator

    A log function would just replace all instances of System.out.println, that way the user can catch it with their plugin and use the information.
     
  7. Offline

    The_Spaceman

    I might be able to create a static method where you can give a runnable with your code in, if that is what you ment...
     
  8. Online

    timtower Ninja on the waves Moderator

    Well, how would somebody read the values now?
     
  9. Offline

    The_Spaceman

    I was thinking replacing the System.out.println, but this only works 1 time, because I have no idea of how to returning the values of the sendMessage methods this is it...
     
  10. Online

    timtower Ninja on the waves Moderator

    You don't need to return them, you need to send them to a method the user can provide.
     
  11. Offline

    The_Spaceman

    how? as far as I know you can put a String in a runnable...
     
  12. Online

    timtower Ninja on the waves Moderator

    There are many ways to pass this along. Runnable is a way, don't see why you need a runnable though as it is sync already.
     
  13. Offline

    The_Spaceman

    Bukkit.dispatchCommand(new CustomCommandSender(player), "locate Monument");

    so I have this for example, where does the String of the sendMessage come from? how do I get this?
    maybe I can create a method that return a ID, and with that ID I can save the String from the sendMessage in a HashMap, and the user can with the ID get the String from that map...
     
  14. Online

    timtower Ninja on the waves Moderator

    @The_Spaceman If you have an interface that accepts the message from sendMessage then the user can handle it however they want.
    Not to forget that it is a synchronized action.
     

Share This Page