Advertisement
Guest User

plugins printing lists of key-block applicability (BROKEN)

a guest
Mar 26th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  21. import java.io.FileNotFoundException;
  22. import java.io.PrintWriter;
  23. import java.lang.reflect.Field;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import java.util.regex.Pattern;
  29.  
  30. @Plugin(id = "mytestplugin42", name = "My Test plugin 42", version = "1.0")
  31. public class MyTestPlugin42
  32. {
  33.     @Inject
  34.     private Logger logger;
  35.  
  36.     @Inject
  37.     private Game game;
  38.  
  39.     private CommandResult cmdOnPrintBlockData(CommandSource src, CommandContext args) throws CommandException
  40.     {
  41.         StringBuilder sb = new StringBuilder();
  42.  
  43.         sb.append("\r\n");
  44.  
  45.         Class<BlockTypes> c = BlockTypes.class;
  46.  
  47.         try {
  48.             Field[] fields = c.getDeclaredFields();
  49.             for(Field f : fields){
  50.                 String blockName = f.getName();
  51.                 BlockType blockType = null;
  52.                 blockType = (BlockType)f.get(c);
  53.                 sb.append(blockName);
  54.                 sb.append("\r\n");
  55.  
  56.                 BlockState blockState = blockType.getDefaultState();
  57.  
  58.                 Class<Keys> kc = Keys.class;
  59.                 Field[] keyFields = kc.getDeclaredFields();
  60.                 for (Field kf : keyFields){
  61.                     String keyName = kf.getName();
  62.                     Key key = null;
  63.                     key = (Key)kf.get(kc);
  64.  
  65.                     if (key == null) continue;
  66.  
  67.  
  68.  
  69.                     if (blockState.supports(key)) {
  70.                         sb.append("    ");
  71.                         sb.append(keyName);
  72.  
  73.                         //Class<? extends BaseValue> valueClass = key.getValueClass();
  74.                         /*BaseValue value = (BaseValue)blockState.get(key).orElse(null);
  75.  
  76.                         if (value==null) continue;
  77.                         if (!(value instanceof Value)) continue;
  78.  
  79.                         Object o = value.get();*/
  80.                         //----
  81.                         Object o = blockState.get(key).orElse(null);
  82.                         //----
  83.                         if ((o==null) ||
  84.                                 (o instanceof Integer) ||
  85.                                 (o instanceof Boolean) ||
  86.                                 (o instanceof Map) ||
  87.                                 (o instanceof List) ||
  88.                                 (o instanceof Set))
  89.                         {
  90.                             sb.append("\r\n");
  91.                             continue;
  92.                         }
  93.                         List<String> possibleValueList = new ArrayList<>();
  94.                         int actualValueCount = 0;
  95.  
  96.                         Field[] valueFields = o.getClass().getDeclaredFields();
  97.                         for (Field vf : valueFields){
  98.                             boolean isApplicable;
  99.                             String valueName = vf.getName();
  100.                             if (Pattern.compile(".*[a-z$].*").matcher(valueName).matches()) continue;
  101.                             if (!vf.isEnumConstant()) continue;
  102.                             Object val = vf.get(o.getClass());
  103.  
  104.                             try {
  105.                                 BlockState tmpBlockState = (BlockState)blockState.with(key, val).orElse(null);
  106.                                 isApplicable = (tmpBlockState != null) && tmpBlockState.get(key).get().equals(val);
  107.                             }
  108.                             catch (IllegalArgumentException e) {
  109.                                 isApplicable = false;
  110.                             }
  111.                             possibleValueList.add((isApplicable?'+':'-') + valueName);
  112.                             if (isApplicable) actualValueCount++;
  113.                         }
  114.  
  115.  
  116.                         if (actualValueCount>=possibleValueList.size()) {
  117.                             sb.append(" (All)");
  118.                         }
  119.                         sb.append("\r\n");
  120.  
  121.                         if (actualValueCount<possibleValueList.size()) {
  122.                             for (String s : possibleValueList) {
  123.                                 sb.append("        ");
  124.                                 sb.append(s);
  125.                                 sb.append("\r\n");
  126.                             }
  127.                         }
  128.                     }
  129.                 }
  130.                 sb.append("\r\n");
  131.             }
  132.         } catch (IllegalAccessException e) {
  133.             e.printStackTrace();
  134.         }
  135.  
  136.         String s = sb.toString();
  137.  
  138.         try {
  139.             PrintWriter out = new PrintWriter("!KeysForBlocks.txt");
  140.             out.println(s);
  141.             out.close();
  142.         } catch (FileNotFoundException e) {
  143.             e.printStackTrace();
  144.         }
  145.         return CommandResult.success();
  146.     }
  147.  
  148.     private CommandResult cmdOnPrintDataOfBlock(CommandSource src, CommandContext args) throws CommandException
  149.     {
  150.         StringBuilder sb = new StringBuilder();
  151.  
  152.         sb.append("\r\n");
  153.  
  154.         try {
  155.             Class<Keys> kc = Keys.class;
  156.             Field[] keyFields = kc.getDeclaredFields();
  157.             for (Field kf : keyFields){
  158.                 String keyName = kf.getName();
  159.                 Key key = null;
  160.                 key = (Key)kf.get(kc);
  161.  
  162.                 if (key == null) continue;
  163.  
  164.                 Class<BlockTypes> c = BlockTypes.class;
  165.  
  166.                 List<String> supportedBlocks = new ArrayList<>();
  167.  
  168.                 Field[] fields = c.getDeclaredFields();
  169.                 for(Field f : fields){
  170.                     String blockName = f.getName();
  171.                     BlockType blockType = null;
  172.                     blockType = (BlockType)f.get(c);
  173.  
  174.                     BlockState blockState = blockType.getDefaultState();
  175.  
  176.                     if (blockState.supports(key)) {
  177.                         supportedBlocks.add(blockName);
  178.                     }
  179.                 }
  180.  
  181.                 if (supportedBlocks.size()>0) {
  182.                     sb.append(keyName);
  183.                     sb.append("\r\n");
  184.  
  185.                     for (String supportedBlock : supportedBlocks) {
  186.                         sb.append("    ");
  187.                         sb.append(supportedBlock);
  188.                         sb.append("\r\n");
  189.                     }
  190.                     sb.append("\r\n");
  191.                 }
  192.             }
  193.         } catch (IllegalAccessException e) {
  194.             e.printStackTrace();
  195.         }
  196.  
  197.         String s = sb.toString();
  198.  
  199.         try {
  200.             PrintWriter out = new PrintWriter("!BlocksForKeys.txt");
  201.             out.println(s);
  202.             out.close();
  203.         } catch (FileNotFoundException e) {
  204.             e.printStackTrace();
  205.         }
  206.         return CommandResult.success();
  207.     }
  208.  
  209.     @Listener
  210.     public void onGameInitialization(GameInitializationEvent event)
  211.     {
  212.         CommandSpec myCommandSpec = CommandSpec.builder()
  213.                 .executor(this::cmdOnPrintBlockData)
  214.                 .build();
  215.  
  216.         CommandSpec myDataSpec = CommandSpec.builder()
  217.                 .executor(this::cmdOnPrintDataOfBlock)
  218.                 .build();
  219.  
  220.         game.getCommandManager().register(this, myCommandSpec, "printblockdata", "printbd");
  221.         game.getCommandManager().register(this, myDataSpec, "printdatablock", "printdb");
  222.  
  223.         logger.info("PEW!");
  224.     }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement