package me.km.commands; import me.km.api.Utils; import me.km.api.GlobalText; import me.km.api.Module; import me.km.api.ModuleCommand; import me.km.permissions.Permissions; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityLiving; import net.minecraft.entity.effect.EntityLightningBolt; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.JsonToNBT; import net.minecraft.nbt.NBTException; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraft.world.World; import net.minecraft.world.chunk.storage.AnvilChunkLoader; public class CommandSummon extends ModuleCommand { public CommandSummon(Module m) { super("summon", m); super.setDescription("Spawnt ein Mob / Mobs"); super.setUsage("/summon [amount] [data]"); super.setPermission(Permissions.SUMMON); } @Override public boolean execute(ICommandSender cs, String[] arg) { if(!(cs instanceof EntityPlayer)) { this.getModule().send(cs, GlobalText.onlyPlayer()); return true; } if(arg.length < 1) { return false; } EntityPlayer p = (EntityPlayer) cs; int counter = 1; if(arg.length >= 2) { try { counter = Integer.parseInt(arg[1]); } catch(NumberFormatException ex) { } } BlockPos target = Utils.getPlayerTarget(p).add(0, 1, 0); String s = arg[0]; double d0 = target.getX(); double d1 = target.getY(); double d2 = target.getZ(); World world = p.getEntityWorld(); try { if (!world.isBlockLoaded(target)) { this.getModule().send(cs, "Der Spawnpunkt ist außerhalb der Welt."); return true; } else if (EntityList.LIGHTNING_BOLT.equals(new ResourceLocation(s))) { world.addWeatherEffect(new EntityLightningBolt(world, d0, d1, d2, false)); } else { NBTTagCompound nbttagcompound = new NBTTagCompound(); boolean flag = false; if (arg.length >= 3) { ITextComponent itextcomponent = getChatComponentFromNthArg(cs, arg, 2); try { nbttagcompound = JsonToNBT.getTagFromJson(itextcomponent.getUnformattedText()); flag = true; } catch (NBTException nbtexception) { this.getModule().send(cs, "Der Tag ist ungültig."); return true; } } nbttagcompound.setString("id", s); for(int i = 0; i < counter; i++) { Entity entity = AnvilChunkLoader.readWorldEntityPos(nbttagcompound, world, d0, d1, d2, true); if (entity == null) { this.getModule().send(cs, "Die Entity lässt sich nicht spawnen."); return true; } else { entity.setLocationAndAngles(d0, d1, d2, entity.rotationYaw, entity.rotationPitch); if (!flag && entity instanceof EntityLiving) { ((EntityLiving) entity).onInitialSpawn(world.getDifficultyForLocation(new BlockPos(entity)), null); } } } } } catch(CommandException ex) { this.getModule().send(cs, "Die Parameter konnten nicht gelesen werden."); } return true; } }