Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test;
- import com.google.inject.Inject;
- import org.slf4j.Logger;
- import org.spongepowered.api.Game;
- import org.spongepowered.api.block.BlockState;
- import org.spongepowered.api.block.BlockType;
- import org.spongepowered.api.block.BlockTypes;
- import org.spongepowered.api.command.CommandException;
- import org.spongepowered.api.command.CommandResult;
- import org.spongepowered.api.command.CommandSource;
- import org.spongepowered.api.command.args.CommandContext;
- import org.spongepowered.api.command.spec.CommandSpec;
- import org.spongepowered.api.data.key.Key;
- import org.spongepowered.api.data.key.Keys;
- import org.spongepowered.api.event.Listener;
- import org.spongepowered.api.event.block.InteractBlockEvent;
- import org.spongepowered.api.event.game.state.GameInitializationEvent;
- import org.spongepowered.api.plugin.Plugin;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.lang.reflect.Field;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.regex.Pattern;
- @Plugin(id = "mytestplugin42", name = "My Test plugin 42", version = "1.0")
- public class MyTestPlugin42
- {
- @Inject
- private Logger logger;
- @Inject
- private Game game;
- private CommandResult cmdOnPrintBlockData(CommandSource src, CommandContext args) throws CommandException
- {
- StringBuilder sb = new StringBuilder();
- sb.append("\r\n");
- Class<BlockTypes> c = BlockTypes.class;
- try {
- Field[] fields = c.getDeclaredFields();
- for(Field f : fields){
- String blockName = f.getName();
- BlockType blockType = null;
- blockType = (BlockType)f.get(c);
- sb.append(blockName);
- sb.append("\r\n");
- BlockState blockState = blockType.getDefaultState();
- Class<Keys> kc = Keys.class;
- Field[] keyFields = kc.getDeclaredFields();
- for (Field kf : keyFields){
- String keyName = kf.getName();
- Key key = null;
- key = (Key)kf.get(kc);
- if (key == null) continue;
- if (blockState.supports(key)) {
- sb.append(" ");
- sb.append(keyName);
- //Class<? extends BaseValue> valueClass = key.getValueClass();
- /*BaseValue value = (BaseValue)blockState.get(key).orElse(null);
- if (value==null) continue;
- if (!(value instanceof Value)) continue;
- Object o = value.get();*/
- //----
- Object o = blockState.get(key).orElse(null);
- //----
- if ((o==null) ||
- (o instanceof Integer) ||
- (o instanceof Boolean) ||
- (o instanceof Map) ||
- (o instanceof List) ||
- (o instanceof Set))
- {
- sb.append("\r\n");
- continue;
- }
- List<String> possibleValueList = new ArrayList<>();
- int actualValueCount = 0;
- Field[] valueFields = o.getClass().getDeclaredFields();
- for (Field vf : valueFields){
- boolean isApplicable;
- String valueName = vf.getName();
- if (Pattern.compile(".*[a-z$].*").matcher(valueName).matches()) continue;
- if (!vf.isEnumConstant()) continue;
- Object val = vf.get(o.getClass());
- try {
- BlockState tmpBlockState = (BlockState)blockState.with(key, val).orElse(null);
- isApplicable = (tmpBlockState != null) && tmpBlockState.get(key).get().equals(val);
- }
- catch (IllegalArgumentException e) {
- isApplicable = false;
- }
- possibleValueList.add((isApplicable?'+':'-') + valueName);
- if (isApplicable) actualValueCount++;
- }
- if (actualValueCount>=possibleValueList.size()) {
- sb.append(" (All)");
- }
- sb.append("\r\n");
- if (actualValueCount<possibleValueList.size()) {
- for (String s : possibleValueList) {
- sb.append(" ");
- sb.append(s);
- sb.append("\r\n");
- }
- }
- }
- }
- sb.append("\r\n");
- }
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- String s = sb.toString();
- try {
- PrintWriter out = new PrintWriter("!KeysForBlocks.txt");
- out.println(s);
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- return CommandResult.success();
- }
- private CommandResult cmdOnPrintDataOfBlock(CommandSource src, CommandContext args) throws CommandException
- {
- StringBuilder sb = new StringBuilder();
- sb.append("\r\n");
- try {
- Class<Keys> kc = Keys.class;
- Field[] keyFields = kc.getDeclaredFields();
- for (Field kf : keyFields){
- String keyName = kf.getName();
- Key key = null;
- key = (Key)kf.get(kc);
- if (key == null) continue;
- Class<BlockTypes> c = BlockTypes.class;
- List<String> supportedBlocks = new ArrayList<>();
- Field[] fields = c.getDeclaredFields();
- for(Field f : fields){
- String blockName = f.getName();
- BlockType blockType = null;
- blockType = (BlockType)f.get(c);
- BlockState blockState = blockType.getDefaultState();
- if (blockState.supports(key)) {
- supportedBlocks.add(blockName);
- }
- }
- if (supportedBlocks.size()>0) {
- sb.append(keyName);
- sb.append("\r\n");
- for (String supportedBlock : supportedBlocks) {
- sb.append(" ");
- sb.append(supportedBlock);
- sb.append("\r\n");
- }
- sb.append("\r\n");
- }
- }
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- String s = sb.toString();
- try {
- PrintWriter out = new PrintWriter("!BlocksForKeys.txt");
- out.println(s);
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- return CommandResult.success();
- }
- @Listener
- public void onGameInitialization(GameInitializationEvent event)
- {
- CommandSpec myCommandSpec = CommandSpec.builder()
- .executor(this::cmdOnPrintBlockData)
- .build();
- CommandSpec myDataSpec = CommandSpec.builder()
- .executor(this::cmdOnPrintDataOfBlock)
- .build();
- game.getCommandManager().register(this, myCommandSpec, "printblockdata", "printbd");
- game.getCommandManager().register(this, myDataSpec, "printdatablock", "printdb");
- logger.info("PEW!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement