hassansyyid

Untitled

Aug 3rd, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.17 KB | None | 0 0
  1. package io.github.hsyyid.spongeessentialcmds;
  2.  
  3. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.BackExecutor;
  4. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.BroadcastExecutor;
  5. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.DeleteHomeExecutor;
  6. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.DeleteWarpExecutor;
  7. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.FeedExecutor;
  8. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.FlyExecutor;
  9. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.HealExecutor;
  10. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.HomeExecutor;
  11. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.JumpExecutor;
  12. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.ListHomeExecutor;
  13. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.ListWarpExecutor;
  14. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.SetHomeExecutor;
  15. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.SetSpawnExecutor;
  16. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.SetWarpExecutor;
  17. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.SpawnExecutor;
  18. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.TPAAcceptExecutor;
  19. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.TPADenyExecutor;
  20. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.TPAExecutor;
  21. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.TPAHereExecutor;
  22. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.TPHereExecutor;
  23. import io.github.hsyyid.spongeessentialcmds.cmdexecutors.WarpExecutor;
  24. import io.github.hsyyid.spongeessentialcmds.events.TPAAcceptEvent;
  25. import io.github.hsyyid.spongeessentialcmds.events.TPAEvent;
  26. import io.github.hsyyid.spongeessentialcmds.events.TPAHereAcceptEvent;
  27. import io.github.hsyyid.spongeessentialcmds.events.TPAHereEvent;
  28. import io.github.hsyyid.spongeessentialcmds.utils.PendingInvitation;
  29. import io.github.hsyyid.spongeessentialcmds.utils.Utils;
  30.  
  31. import java.io.File;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34. import java.util.concurrent.TimeUnit;
  35.  
  36. import ninja.leaping.configurate.ConfigurationNode;
  37. import ninja.leaping.configurate.commented.CommentedConfigurationNode;
  38. import ninja.leaping.configurate.loader.ConfigurationLoader;
  39.  
  40. import org.slf4j.Logger;
  41. import org.spongepowered.api.Game;
  42. import org.spongepowered.api.block.tileentity.TileEntity;
  43. import org.spongepowered.api.block.tileentity.TileEntityTypes;
  44. import org.spongepowered.api.data.manipulator.DisplayNameData;
  45. import org.spongepowered.api.data.manipulator.tileentity.SignData;
  46. import org.spongepowered.api.entity.player.Player;
  47. import org.spongepowered.api.event.Subscribe;
  48. import org.spongepowered.api.event.block.tileentity.SignChangeEvent;
  49. import org.spongepowered.api.event.entity.player.PlayerChatEvent;
  50. import org.spongepowered.api.event.entity.player.PlayerDeathEvent;
  51. import org.spongepowered.api.event.entity.player.PlayerInteractBlockEvent;
  52. import org.spongepowered.api.event.entity.player.PlayerJoinEvent;
  53. import org.spongepowered.api.event.entity.player.PlayerMoveEvent;
  54. import org.spongepowered.api.event.state.ServerStartedEvent;
  55. import org.spongepowered.api.plugin.Plugin;
  56. import org.spongepowered.api.service.command.CommandService;
  57. import org.spongepowered.api.service.config.DefaultConfig;
  58. import org.spongepowered.api.service.permission.Subject;
  59. import org.spongepowered.api.service.permission.option.OptionSubject;
  60. import org.spongepowered.api.service.scheduler.SchedulerService;
  61. import org.spongepowered.api.service.scheduler.Task;
  62. import org.spongepowered.api.service.scheduler.TaskBuilder;
  63. import org.spongepowered.api.text.Texts;
  64. import org.spongepowered.api.text.format.TextColors;
  65. import org.spongepowered.api.util.command.args.GenericArguments;
  66. import org.spongepowered.api.util.command.spec.CommandSpec;
  67. import org.spongepowered.api.world.Location;
  68. import org.spongepowered.api.world.TeleportHelper;
  69.  
  70. import com.google.common.base.Optional;
  71. import com.google.inject.Inject;
  72.  
  73. @Plugin(id = "SpongeEssentialCmds", name = "SpongeEssentialCmds", version = "1.6")
  74. public class Main
  75. {
  76.     public static Game game = null;
  77.     public static ConfigurationNode config = null;
  78.     public static ConfigurationLoader<CommentedConfigurationNode> configurationManager;
  79.     public static TeleportHelper helper;
  80.     public static ArrayList<PendingInvitation> pendingInvites = new ArrayList<PendingInvitation>();
  81.  
  82.     @Inject
  83.     private Logger logger;
  84.  
  85.     public Logger getLogger()
  86.     {
  87.         return logger;
  88.     }
  89.  
  90.     @Inject
  91.     @DefaultConfig(sharedRoot = true)
  92.     private File dConfig;
  93.  
  94.     @Inject
  95.     @DefaultConfig(sharedRoot = true)
  96.     private ConfigurationLoader<CommentedConfigurationNode> confManager;
  97.  
  98.     @Subscribe
  99.     public void onServerStart(ServerStartedEvent event)
  100.     {
  101.         getLogger().info("SpongeEssentialCmds loading...");
  102.         game = event.getGame();
  103.         helper = game.getTeleportHelper();
  104.         // Config File
  105.         try
  106.         {
  107.             if (!dConfig.exists())
  108.             {
  109.                 dConfig.createNewFile();
  110.                 config = confManager.load();
  111.                 config.getNode("home", "users", "HassanS6000", "home", "X").setValue(0);
  112.                 config.getNode("home", "users", "HassanS6000", "home", "Y").setValue(0);
  113.                 config.getNode("home", "users", "HassanS6000", "home", "Z").setValue(0);
  114.                 config.getNode("home", "users", "HassanS6000", "homes").setValue("home,");
  115.                 confManager.save(config);
  116.             }
  117.             configurationManager = confManager;
  118.             config = confManager.load();
  119.  
  120.         }
  121.         catch (IOException exception)
  122.         {
  123.             getLogger().error("The default configuration could not be loaded or created!");
  124.         }
  125.  
  126.         CommandSpec homeCommandSpec = CommandSpec.builder()
  127.             .description(Texts.of("Home Command"))
  128.             .permission("home.use")
  129.             .arguments(GenericArguments.onlyOne(GenericArguments.string(Texts.of("home name"))))
  130.             .executor(new HomeExecutor())
  131.             .build();
  132.  
  133.         game.getCommandDispatcher().register(this, homeCommandSpec, "home");
  134.  
  135.         CommandSpec broadcastCommandSpec = CommandSpec.builder()
  136.             .description(Texts.of("Broadcast Command"))
  137.             .permission("broadcast.use")
  138.             .arguments(GenericArguments.remainingJoinedStrings(Texts.of("message")))
  139.             .executor(new BroadcastExecutor())
  140.             .build();
  141.  
  142.         game.getCommandDispatcher().register(this, broadcastCommandSpec, "broadcast");
  143.  
  144.         CommandSpec spawnCommandSpec = CommandSpec.builder()
  145.             .description(Texts.of("Spawn Command"))
  146.             .permission("spawn.use")
  147.             .executor(new SpawnExecutor())
  148.             .build();
  149.  
  150.         game.getCommandDispatcher().register(this, spawnCommandSpec, "spawn");
  151.  
  152.         CommandSpec setSpawnCommandSpec = CommandSpec.builder()
  153.             .description(Texts.of("Spawn Command"))
  154.             .permission("spawn.set")
  155.             .executor(new SetSpawnExecutor())
  156.             .build();
  157.  
  158.         game.getCommandDispatcher().register(this, setSpawnCommandSpec, "setspawn");
  159.  
  160.         CommandSpec tpaCommandSpec = CommandSpec.builder()
  161.             .description(Texts.of("TPA Command"))
  162.             .permission("tpa.use")
  163.             .arguments(GenericArguments.onlyOne(GenericArguments.player(Texts.of("player"), game)))
  164.             .executor(new TPAExecutor())
  165.             .build();
  166.  
  167.         game.getCommandDispatcher().register(this, tpaCommandSpec, "tpa");
  168.  
  169.         CommandSpec tpaHereCommandSpec = CommandSpec.builder()
  170.             .description(Texts.of("TPA Here Command"))
  171.             .permission("tpahere.use")
  172.             .arguments(GenericArguments.onlyOne(GenericArguments.player(Texts.of("player"), game)))
  173.             .executor(new TPAHereExecutor())
  174.             .build();
  175.  
  176.         game.getCommandDispatcher().register(this, tpaHereCommandSpec, "tpahere");
  177.  
  178.         CommandSpec tpHereCommandSpec = CommandSpec.builder()
  179.             .description(Texts.of("TP Here Command"))
  180.             .permission("tphere.use")
  181.             .arguments(GenericArguments.onlyOne(GenericArguments.player(Texts.of("player"), game)))
  182.             .executor(new TPHereExecutor())
  183.             .build();
  184.  
  185.         game.getCommandDispatcher().register(this, tpHereCommandSpec, "tphere");
  186.  
  187.         CommandSpec tpaAcceptCommandSpec = CommandSpec.builder()
  188.             .description(Texts.of("TPA Accept Command"))
  189.             .permission("tpa.accept")
  190.             .executor(new TPAAcceptExecutor())
  191.             .build();
  192.  
  193.         game.getCommandDispatcher().register(this, tpaAcceptCommandSpec, "tpaccept");
  194.  
  195.         CommandSpec listHomeCommandSpec = CommandSpec.builder()
  196.             .description(Texts.of("List Home Command"))
  197.             .permission("home.list")
  198.             .arguments(GenericArguments.optional(GenericArguments.onlyOne(GenericArguments.integer(Texts.of("page no")))))
  199.             .executor(new ListHomeExecutor())
  200.             .build();
  201.  
  202.         game.getCommandDispatcher().register(this, listHomeCommandSpec, "homes");
  203.  
  204.         CommandSpec healCommandSpec = CommandSpec.builder()
  205.             .description(Texts.of("Heal Command"))
  206.             .permission("heal.use")
  207.             .executor(new HealExecutor())
  208.             .build();
  209.  
  210.         game.getCommandDispatcher().register(this, healCommandSpec, "heal");
  211.  
  212.         CommandSpec backCommandSpec = CommandSpec.builder()
  213.             .description(Texts.of("Back Command"))
  214.             .permission("back.use")
  215.             .executor(new BackExecutor())
  216.             .build();
  217.  
  218.         game.getCommandDispatcher().register(this, backCommandSpec, "back");
  219.  
  220.         CommandSpec tpaDenyCommandSpec = CommandSpec.builder()
  221.             .description(Texts.of("TPA Deny Command"))
  222.             .permission("tpadeny.use")
  223.             .executor(new TPADenyExecutor())
  224.             .build();
  225.  
  226.         game.getCommandDispatcher().register(this, tpaDenyCommandSpec, "tpadeny");
  227.  
  228.         CommandSpec flyCommandSpec = CommandSpec.builder()
  229.             .description(Texts.of("Fly Command"))
  230.             .permission("fly.use")
  231.             .executor(new FlyExecutor())
  232.             .build();
  233.  
  234.         game.getCommandDispatcher().register(this, flyCommandSpec, "fly");
  235.  
  236.         CommandSpec setHomeCommandSpec = CommandSpec.builder()
  237.             .description(Texts.of("Set Home Command"))
  238.             .permission("home.set")
  239.             .arguments(GenericArguments.onlyOne(GenericArguments.string(Texts.of("home name"))))
  240.             .executor(new SetHomeExecutor())
  241.             .build();
  242.  
  243.         game.getCommandDispatcher().register(this, setHomeCommandSpec, "sethome");
  244.  
  245.         CommandSpec deleteHomeCommandSpec = CommandSpec.builder()
  246.             .description(Texts.of("Delete Home Command"))
  247.             .permission("home.delete")
  248.             .arguments(GenericArguments.onlyOne(GenericArguments.string(Texts.of("home name"))))
  249.             .executor(new DeleteHomeExecutor())
  250.             .build();
  251.  
  252.         game.getCommandDispatcher().register(this, deleteHomeCommandSpec, "deletehome", "delhome");
  253.  
  254.         CommandSpec warpCommandSpec = CommandSpec.builder()
  255.             .description(Texts.of("Warp Command"))
  256.             .permission("warp.use")
  257.             .arguments(GenericArguments.onlyOne(GenericArguments.string(Texts.of("warp name"))))
  258.             .executor(new WarpExecutor())
  259.             .build();
  260.  
  261.         game.getCommandDispatcher().register(this, warpCommandSpec, "warp");
  262.  
  263.         CommandSpec listWarpCommandSpec = CommandSpec.builder()
  264.             .description(Texts.of("List Warps Command"))
  265.             .permission("warps.list")
  266.             .arguments(GenericArguments.optional(GenericArguments.onlyOne(GenericArguments.integer(Texts.of("page no")))))
  267.             .executor(new ListWarpExecutor())
  268.             .build();
  269.  
  270.         game.getCommandDispatcher().register(this, listWarpCommandSpec, "warps");
  271.  
  272.         CommandSpec setWarpCommandSpec = CommandSpec.builder()
  273.             .description(Texts.of("Set Warp Command"))
  274.             .permission("warp.set")
  275.             .arguments(GenericArguments.onlyOne(GenericArguments.string(Texts.of("warp name"))))
  276.             .executor(new SetWarpExecutor())
  277.             .build();
  278.  
  279.         game.getCommandDispatcher().register(this, setWarpCommandSpec, "setwarp");
  280.  
  281.         CommandSpec deleteWarpCommandSpec = CommandSpec.builder()
  282.             .description(Texts.of("Delete Warp Command"))
  283.             .permission("warp.delete")
  284.             .arguments(GenericArguments.onlyOne(GenericArguments.string(Texts.of("warp name"))))
  285.             .executor(new DeleteWarpExecutor())
  286.             .build();
  287.  
  288.         game.getCommandDispatcher().register(this, deleteWarpCommandSpec, "deletewarp", "delwarp");
  289.  
  290.         CommandSpec feedCommandSpec = CommandSpec.builder()
  291.             .description(Texts.of("Feed Command"))
  292.             .permission("feed.use")
  293.             .executor(new FeedExecutor())
  294.             .build();
  295.  
  296.         game.getCommandDispatcher().register(this, feedCommandSpec, "feed");
  297.  
  298.         CommandSpec jumpCommandSpec = CommandSpec.builder()
  299.             .description(Texts.of("Jump Command"))
  300.             .permission("jump.use")
  301.             .executor(new JumpExecutor())
  302.             .build();
  303.  
  304.         game.getCommandDispatcher().register(this, jumpCommandSpec, "jump");
  305.  
  306.         getLogger().info("-----------------------------");
  307.         getLogger().info("SpongeEssentialCmds was made by HassanS6000!");
  308.         getLogger().info("Please post all errors on the Sponge Thread or on GitHub!");
  309.         getLogger().info("Have fun, and enjoy! :D");
  310.         getLogger().info("-----------------------------");
  311.         getLogger().info("SpongeEssentialCmds loaded!");
  312.     }
  313.  
  314.     @Subscribe
  315.     public void onPlayerDeath(PlayerDeathEvent event)
  316.     {
  317.         Player died = event.getEntity();
  318.         Utils.addLastDeathLocation(died.getUniqueId(), died.getLocation());
  319.     }
  320.    
  321.     @Subscribe
  322.     public void onPlayerJoin(PlayerJoinEvent event)
  323.     {
  324.         Player player = event.getEntity();
  325.         Subject subject = player.getContainingCollection().get(player.getIdentifier());
  326.         if(subject instanceof OptionSubject)
  327.         {
  328.             OptionSubject optionSubject = (OptionSubject) subject;
  329.             String prefix = optionSubject.getOption("prefix").or("");
  330.             prefix.replaceAll("&", "\u00A7");
  331.             DisplayNameData data = player.getDisplayNameData();
  332.             data.setDisplayName(Texts.of(prefix + " " + data.getDisplayName().toString()));
  333.             data.setCustomNameVisible(true);
  334.             player.offer(data);
  335.         }
  336.     }
  337.  
  338.     @Subscribe
  339.     public void tpaEventHandler(TPAEvent event)
  340.     {
  341.         String senderName = event.getSender().getName();
  342.         event.getRecipient().sendMessage(Texts.of(TextColors.BLUE, "TPA Request From: ", TextColors.GOLD, senderName + ".", TextColors.RED, " You have 10 seconds to do /tpaccept to accept the request"));
  343.  
  344.         // Adds Invite to List
  345.         final PendingInvitation invite = new PendingInvitation(event.getSender(), event.getRecipient());
  346.         pendingInvites.add(invite);
  347.  
  348.         // Removes Invite after 10 Seconds
  349.         SchedulerService scheduler = game.getScheduler();
  350.         TaskBuilder taskBuilder = scheduler.getTaskBuilder();
  351.  
  352.         Task task = taskBuilder.execute(new Runnable()
  353.         {
  354.             public void run()
  355.             {
  356.                 if (pendingInvites.contains(invite))
  357.                 {
  358.                     pendingInvites.remove(invite);
  359.                 }
  360.             }
  361.         }).delay(10, TimeUnit.SECONDS).name("SpongeEssentialCmds - Remove Pending Invite").submit(game.getPluginManager().getPlugin("SpongeEssentialCmds").get().getInstance());
  362.     }
  363.  
  364.     @Subscribe
  365.     public void tpaAcceptEventHandler(TPAAcceptEvent event)
  366.     {
  367.         String senderName = event.getSender().getName();
  368.         event.getRecipient().sendMessage(Texts.of(TextColors.GREEN, senderName, TextColors.WHITE, " accepted your TPA Request."));
  369.         event.getRecipient().setLocation(event.getSender().getLocation());
  370.     }
  371.  
  372.     @Subscribe
  373.     public void tpaHereAcceptEventHandler(TPAHereAcceptEvent event)
  374.     {
  375.         String recipientName = event.getRecipient().getName();
  376.         event.getSender().sendMessage(Texts.of(TextColors.GREEN, recipientName, TextColors.WHITE, " accepted your TPA Here Request."));
  377.         event.getSender().setLocation(event.getRecipient().getLocation());
  378.     }
  379.  
  380.     @Subscribe
  381.     public void tpaHereEventHandler(TPAHereEvent event)
  382.     {
  383.         String senderName = event.getSender().getName();
  384.         event.getRecipient().sendMessage(Texts.of(TextColors.BLUE, senderName, TextColors.GOLD, " has requested for you to teleport to them.", TextColors.RED, " You have 10 seconds to do /tpaccept to accept the request"));
  385.  
  386.         // Adds Invite to List
  387.         final PendingInvitation invite = new PendingInvitation(event.getSender(), event.getRecipient());
  388.         invite.isTPAHere = true;
  389.         pendingInvites.add(invite);
  390.  
  391.         // Removes Invite after 10 Seconds
  392.         SchedulerService scheduler = game.getScheduler();
  393.         TaskBuilder taskBuilder = scheduler.getTaskBuilder();
  394.  
  395.         Task task = taskBuilder.execute(new Runnable()
  396.         {
  397.             public void run()
  398.             {
  399.                 if (pendingInvites.contains(invite))
  400.                 {
  401.                     pendingInvites.remove(invite);
  402.                 }
  403.             }
  404.         }).delay(10, TimeUnit.SECONDS).name("SpongeEssentialCmds - Remove Pending Invite").submit(game.getPluginManager().getPlugin("SpongeEssentialCmds").get().getInstance());
  405.     }
  406.  
  407.     @Subscribe
  408.     public void onMessage(PlayerChatEvent event)
  409.     {  
  410.         if (event.getEntity().hasPermission("color.chat.use"))
  411.         {
  412.             String original = Texts.toPlain(event.getMessage());
  413.             String newMessage = original.replaceAll("&", "\u00A7");
  414.             event.setNewMessage(Texts.of(newMessage));
  415.         }
  416.     }
  417.  
  418.     @Subscribe
  419.     public void onSignChange(SignChangeEvent event)
  420.     {
  421.         SignData signData = event.getNewData();
  422.         String line0 = Texts.toPlain(signData.getLine(0));
  423.         String line1 = Texts.toPlain(signData.getLine(1));
  424.         String line2 = Texts.toPlain(signData.getLine(2));
  425.         String line3 = Texts.toPlain(signData.getLine(3));
  426.         if(line0.equals("[Warp]"))
  427.         {
  428.             if(Utils.getWarps().contains(line1))
  429.             {
  430.                 signData.setLine(0, Texts.of(TextColors.DARK_BLUE, "[Warp]"));
  431.             }
  432.             else
  433.             {
  434.                 signData.setLine(0, Texts.of(TextColors.DARK_RED, "[Warp]"));
  435.             }
  436.         }
  437.         else
  438.         {
  439.             signData.setLine(0, Texts.of(line0.replaceAll("&", "\u00A7")));
  440.         }
  441.         signData.setLine(1, Texts.of(line1.replaceAll("&", "\u00A7")));
  442.         signData.setLine(2, Texts.of(line2.replaceAll("&", "\u00A7")));
  443.         signData.setLine(3, Texts.of(line3.replaceAll("&", "\u00A7")));
  444.  
  445.         event.setNewData(signData);
  446.     }
  447.  
  448.     @Subscribe
  449.     public void onPlayerInteractBlock(PlayerInteractBlockEvent event)
  450.     {
  451.         Location block = event.getBlock();
  452.         Player player = event.getUser();
  453.  
  454.         if (block.getTileEntity().isPresent())
  455.         {
  456.             TileEntity clickedEntity = block.getTileEntity().get();
  457.             if (clickedEntity.getType() == TileEntityTypes.SIGN)
  458.             {
  459.                 Optional<SignData> data = clickedEntity.getOrCreate(SignData.class);
  460.                 CommandService cmdService = game.getCommandDispatcher();
  461.                 if (data.isPresent())
  462.                 {
  463.                     String line0 = Texts.toPlain(data.get().getLine(0));
  464.                     String line1 = Texts.toPlain(data.get().getLine(1));
  465.                     String command = "warp " + line1;
  466.  
  467.                     if (line0.equals("[Warp]"))
  468.                     {
  469.                         if (player.hasPermission("warps.use.sign"))
  470.                         {
  471.                             cmdService.process(player, command);
  472.                         }
  473.                         else
  474.                         {
  475.                             player.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You do not have permission to use Warp Signs!"));
  476.                         }
  477.                     }
  478.                 }
  479.             }
  480.         }
  481.     }
  482.  
  483.     @Subscribe
  484.     public void onPlayerMove(PlayerMoveEvent event)
  485.     {
  486.         event.getEntity();
  487.     }
  488.    
  489.     public static ConfigurationLoader<CommentedConfigurationNode> getConfigManager()
  490.     {
  491.         return configurationManager;
  492.     }
  493. }
Advertisement
Add Comment
Please, Sign In to add comment