Advertisement
Guest User

plugins printing lists of key-block applicability

a guest
Mar 24th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. package com.test;
  2.  
  3. import com.google.inject.Inject;
  4. import org.slf4j.Logger;
  5. import org.spongepowered.api.Game;
  6. import org.spongepowered.api.block.BlockState;
  7. import org.spongepowered.api.block.BlockType;
  8. import org.spongepowered.api.block.BlockTypes;
  9. import org.spongepowered.api.command.CommandException;
  10. import org.spongepowered.api.command.CommandResult;
  11. import org.spongepowered.api.command.CommandSource;
  12. import org.spongepowered.api.command.args.CommandContext;
  13. import org.spongepowered.api.command.spec.CommandSpec;
  14. import org.spongepowered.api.data.key.Key;
  15. import org.spongepowered.api.data.key.Keys;
  16. import org.spongepowered.api.event.Listener;
  17. import org.spongepowered.api.event.block.InteractBlockEvent;
  18. import org.spongepowered.api.event.game.state.GameInitializationEvent;
  19. import org.spongepowered.api.plugin.Plugin;
  20. import org.spongepowered.api.text.Text;
  21.  
  22. import java.io.FileNotFoundException;
  23. import java.io.PrintWriter;
  24. import java.lang.reflect.Field;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27.  
  28. @Plugin(id = "mytestplugin42", name = "My Test plugin 42", version = "1.0")
  29. public class MyTestPlugin42
  30. {
  31. @Inject
  32. private Logger logger;
  33.  
  34. @Inject
  35. private Game game;
  36.  
  37. private CommandResult cmdOnPrintBlockData(CommandSource src, CommandContext args) throws CommandException
  38. {
  39. StringBuilder sb = new StringBuilder();
  40.  
  41. sb.append("\r\n");
  42.  
  43. Class<BlockTypes> c = BlockTypes.class;
  44.  
  45. try {
  46. Field[] fields = c.getDeclaredFields();
  47. for(Field f : fields){
  48. String blockName = f.getName();
  49. BlockType blockType = null;
  50. blockType = (BlockType)f.get(c);
  51. sb.append(blockName);
  52. sb.append("\r\n");
  53.  
  54. BlockState blockState = blockType.getDefaultState();
  55.  
  56. Class<Keys> kc = Keys.class;
  57. Field[] keyFields = kc.getDeclaredFields();
  58. for (Field kf : keyFields){
  59. String keyName = kf.getName();
  60. Key key = null;
  61. key = (Key)kf.get(kc);
  62.  
  63. if (key == null) continue;
  64.  
  65.  
  66.  
  67. if (blockState.supports(key)) {
  68. sb.append(" ");
  69. sb.append(keyName);
  70. sb.append("\r\n");
  71. }
  72. }
  73. sb.append("\r\n");
  74. }
  75. } catch (IllegalAccessException e) {
  76. e.printStackTrace();
  77. }
  78.  
  79. String s = sb.toString();
  80.  
  81. try {
  82. PrintWriter out = new PrintWriter("!KeysForBlocks.txt");
  83. out.println(s);
  84. out.close();
  85. } catch (FileNotFoundException e) {
  86. e.printStackTrace();
  87. }
  88. return CommandResult.success();
  89. }
  90.  
  91. private CommandResult cmdOnPrintDataOfBlock(CommandSource src, CommandContext args) throws CommandException
  92. {
  93. StringBuilder sb = new StringBuilder();
  94.  
  95. sb.append("\r\n");
  96.  
  97. try {
  98. Class<Keys> kc = Keys.class;
  99. Field[] keyFields = kc.getDeclaredFields();
  100. for (Field kf : keyFields){
  101. String keyName = kf.getName();
  102. Key key = null;
  103. key = (Key)kf.get(kc);
  104.  
  105. if (key == null) continue;
  106.  
  107. Class<BlockTypes> c = BlockTypes.class;
  108.  
  109. List<String> supportedBlocks = new ArrayList<>();
  110.  
  111. Field[] fields = c.getDeclaredFields();
  112. for(Field f : fields){
  113. String blockName = f.getName();
  114. BlockType blockType = null;
  115. blockType = (BlockType)f.get(c);
  116.  
  117. BlockState blockState = blockType.getDefaultState();
  118.  
  119. if (blockState.supports(key)) {
  120. supportedBlocks.add(blockName);
  121. }
  122. }
  123.  
  124. if (supportedBlocks.size()>0) {
  125. sb.append(keyName);
  126. sb.append("\r\n");
  127.  
  128. for (String supportedBlock : supportedBlocks) {
  129. sb.append(" ");
  130. sb.append(supportedBlock);
  131. sb.append("\r\n");
  132. }
  133. sb.append("\r\n");
  134. }
  135. }
  136. } catch (IllegalAccessException e) {
  137. e.printStackTrace();
  138. }
  139.  
  140. String s = sb.toString();
  141.  
  142. try {
  143. PrintWriter out = new PrintWriter("!BlocksForKeys.txt");
  144. out.println(s);
  145. out.close();
  146. } catch (FileNotFoundException e) {
  147. e.printStackTrace();
  148. }
  149. return CommandResult.success();
  150. }
  151.  
  152. @Listener
  153. public void onGameInitialization(GameInitializationEvent event)
  154. {
  155. CommandSpec myCommandSpec = CommandSpec.builder()
  156. .executor(this::cmdOnPrintBlockData)
  157. .build();
  158.  
  159. CommandSpec myDataSpec = CommandSpec.builder()
  160. .executor(this::cmdOnPrintDataOfBlock)
  161. .build();
  162.  
  163. game.getCommandManager().register(this, myCommandSpec, "printblockdata", "printbd");
  164. game.getCommandManager().register(this, myDataSpec, "printdatablock", "printdb");
  165.  
  166. logger.info("PEW!");
  167. }
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement