Advertisement
Earthcomputer

DataCommand.java

Sep 10th, 2018
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.67 KB | None | 0 0
  1. package net.minecraft.command.impl.data;
  2.  
  3. import com.google.common.collect.Lists;
  4. import com.mojang.brigadier.CommandDispatcher;
  5. import com.mojang.brigadier.arguments.DoubleArgumentType;
  6. import com.mojang.brigadier.builder.ArgumentBuilder;
  7. import com.mojang.brigadier.builder.LiteralArgumentBuilder;
  8. import com.mojang.brigadier.builder.RequiredArgumentBuilder;
  9. import com.mojang.brigadier.context.CommandContext;
  10. import com.mojang.brigadier.exceptions.CommandSyntaxException;
  11. import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
  12. import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
  13. import java.util.List;
  14. import java.util.function.Function;
  15. import net.minecraft.command.CommandSource;
  16. import net.minecraft.command.Commands;
  17. import net.minecraft.command.arguments.NBTArgument;
  18. import net.minecraft.command.arguments.NBTPathArgument;
  19. import net.minecraft.nbt.INBTBase;
  20. import net.minecraft.nbt.NBTPrimitive;
  21. import net.minecraft.nbt.NBTTagCollection;
  22. import net.minecraft.nbt.NBTTagCompound;
  23. import net.minecraft.nbt.NBTTagString;
  24. import net.minecraft.util.math.MathHelper;
  25. import net.minecraft.util.text.TextComponentTranslation;
  26.  
  27. public class DataCommand {
  28.     private static final SimpleCommandExceptionType MERGE_FAILED_EXCEPTION = new SimpleCommandExceptionType(new TextComponentTranslation("commands.data.merge.failed", new Object[0]));
  29.     private static final DynamicCommandExceptionType GET_INVALID_EXCEPTION = new DynamicCommandExceptionType((p_208922_0_) -> {
  30.         return new TextComponentTranslation("commands.data.get.invalid", new Object[]{p_208922_0_});
  31.     });
  32.     private static final DynamicCommandExceptionType GET_UNKNOWN_EXCEPTION = new DynamicCommandExceptionType((p_208919_0_) -> {
  33.         return new TextComponentTranslation("commands.data.get.unknown", new Object[]{p_208919_0_});
  34.     });
  35.     public static final List<DataCommand.IDataProvider> DATA_PROVIDERS = Lists.newArrayList(EntityDataAccessor.DATA_PROVIDER, BlockDataAccessor.DATA_PROVIDER);
  36.  
  37.     public static void register(CommandDispatcher<CommandSource> dispatcher) {
  38.         LiteralArgumentBuilder<CommandSource> literalargumentbuilder = (LiteralArgumentBuilder)Commands.literal("data").requires((p_198939_0_) -> {
  39.             return p_198939_0_.hasPermissionLevel(2);
  40.         });
  41.  
  42.         for (DataCommand.IDataProvider datacommand$idataprovider : DATA_PROVIDERS) {
  43.             ((LiteralArgumentBuilder)((LiteralArgumentBuilder)literalargumentbuilder.then(datacommand$idataprovider.func_198920_a(Commands.literal("merge"), (p_198943_1_) -> {
  44.                 return p_198943_1_.then(Commands.argument("nbt", NBTArgument.nbt()).executes((p_198936_1_) -> {
  45.                     return dataMergeAll((CommandSource)p_198936_1_.getSource(), datacommand$idataprovider.func_198919_a(p_198936_1_), NBTArgument.func_197130_a(p_198936_1_, "nbt"));
  46.                 }));
  47.             }))).then(datacommand$idataprovider.func_198920_a(Commands.literal("get"), (p_198940_1_) -> {
  48.                 return p_198940_1_.executes((p_198944_1_) -> {
  49.                     return dataGetAll((CommandSource)p_198944_1_.getSource(), datacommand$idataprovider.func_198919_a(p_198944_1_));
  50.                 }).then(((RequiredArgumentBuilder)Commands.argument("path", NBTPathArgument.nbtPath()).executes((p_198945_1_) -> {
  51.                     return dataGet((CommandSource)p_198945_1_.getSource(), datacommand$idataprovider.func_198919_a(p_198945_1_), NBTPathArgument.getNBTPath(p_198945_1_, "path"));
  52.                 })).then(Commands.argument("scale", DoubleArgumentType.doubleArg()).executes((p_198935_1_) -> {
  53.                     return func_198938_a((CommandSource)p_198935_1_.getSource(), datacommand$idataprovider.func_198919_a(p_198935_1_), NBTPathArgument.getNBTPath(p_198935_1_, "path"), DoubleArgumentType.getDouble(p_198935_1_, "scale"));
  54.                 })));
  55.             }))).then(datacommand$idataprovider.func_198920_a(Commands.literal("remove"), (p_198934_1_) -> {
  56.                 return p_198934_1_.then(Commands.argument("path", NBTPathArgument.nbtPath()).executes((p_198941_1_) -> {
  57.                     return dataMerge((CommandSource)p_198941_1_.getSource(), datacommand$idataprovider.func_198919_a(p_198941_1_), NBTPathArgument.getNBTPath(p_198941_1_, "path"));
  58.                 }));
  59.             }));
  60.         }
  61.  
  62.         dispatcher.register(literalargumentbuilder);
  63.     }
  64.  
  65.     private static int dataMerge(CommandSource source, IDataAccessor accessor, NBTPathArgument.NBTPath pathIn) throws CommandSyntaxException {
  66.         NBTTagCompound nbttagcompound = accessor.getData();
  67.         NBTTagCompound nbttagcompound1 = nbttagcompound.copy();
  68.         pathIn.func_197140_b(nbttagcompound);
  69.  
  70.         if (nbttagcompound1.equals(nbttagcompound)) {
  71.             throw MERGE_FAILED_EXCEPTION.create();
  72.         } else {
  73.             accessor.mergeData(nbttagcompound);
  74.             source.sendFeedback(accessor.getModifiedMessage(), true);
  75.             return 1;
  76.         }
  77.     }
  78.  
  79.     private static int dataGet(CommandSource p_201228_0_, IDataAccessor p_201228_1_, NBTPathArgument.NBTPath p_201228_2_) throws CommandSyntaxException {
  80.         INBTBase inbtbase = p_201228_2_.func_197143_a(p_201228_1_.getData());
  81.         int i;
  82.  
  83.         if (inbtbase instanceof NBTPrimitive) {
  84.             i = MathHelper.floor(((NBTPrimitive)inbtbase).getDouble());
  85.         } else if (inbtbase instanceof NBTTagCollection) {
  86.             i = ((NBTTagCollection)inbtbase).size();
  87.         } else if (inbtbase instanceof NBTTagCompound) {
  88.             i = ((NBTTagCompound)inbtbase).getSize();
  89.         } else {
  90.             if (!(inbtbase instanceof NBTTagString)) {
  91.                 throw GET_UNKNOWN_EXCEPTION.create(p_201228_2_.toString());
  92.             }
  93.  
  94.             i = ((NBTTagString)inbtbase).getString().length();
  95.         }
  96.  
  97.         p_201228_0_.sendFeedback(p_201228_1_.getQueryMessage(inbtbase), false);
  98.         return i;
  99.     }
  100.  
  101.     private static int func_198938_a(CommandSource p_198938_0_, IDataAccessor p_198938_1_, NBTPathArgument.NBTPath p_198938_2_, double p_198938_3_) throws CommandSyntaxException {
  102.         INBTBase inbtbase = p_198938_2_.func_197143_a(p_198938_1_.getData());
  103.  
  104.         if (!(inbtbase instanceof NBTPrimitive)) {
  105.             throw GET_INVALID_EXCEPTION.create(p_198938_2_.toString());
  106.         } else {
  107.             int i = MathHelper.floor(((NBTPrimitive)inbtbase).getDouble() * p_198938_3_);
  108.             p_198938_0_.sendFeedback(p_198938_1_.getGetMessage(p_198938_2_, p_198938_3_, i), false);
  109.             return i;
  110.         }
  111.     }
  112.  
  113.     private static int dataGetAll(CommandSource p_198947_0_, IDataAccessor p_198947_1_) throws CommandSyntaxException {
  114.         p_198947_0_.sendFeedback(p_198947_1_.getQueryMessage(p_198947_1_.getData()), false);
  115.         return 1;
  116.     }
  117.  
  118.     private static int dataMergeAll(CommandSource p_198946_0_, IDataAccessor p_198946_1_, NBTTagCompound p_198946_2_) throws CommandSyntaxException {
  119.         NBTTagCompound nbttagcompound = p_198946_1_.getData();
  120.         NBTTagCompound nbttagcompound1 = nbttagcompound.copy().merge(p_198946_2_);
  121.  
  122.         if (nbttagcompound.equals(nbttagcompound1)) {
  123.             throw MERGE_FAILED_EXCEPTION.create();
  124.         } else {
  125.             p_198946_1_.mergeData(nbttagcompound1);
  126.             p_198946_0_.sendFeedback(p_198946_1_.getModifiedMessage(), true);
  127.             return 1;
  128.         }
  129.     }
  130.  
  131.     public interface IDataProvider {
  132.         IDataAccessor func_198919_a(CommandContext<CommandSource> p_198919_1_) throws CommandSyntaxException;
  133.  
  134.         ArgumentBuilder < CommandSource, ? > func_198920_a(ArgumentBuilder < CommandSource, ? > p_198920_1_, Function < ArgumentBuilder < CommandSource, ? >, ArgumentBuilder < CommandSource, ? >> p_198920_2_);
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement