@Override
  public List<String> getTabCompletionOptions(
      MinecraftServer server,
      ICommandSender iCommandSender,
      String[] typedValue,
      @Nullable BlockPos pos) {
    if (!PlayerMPUtil.isPlayer(iCommandSender)) {
      return null;
    }
    if (typedValue.length == 1) {

      EntityPlayerMP player = (EntityPlayerMP) PlayerMPUtil.castToPlayer(iCommandSender);
      if (player == null) {
        return null;
      }

      ItemStack stack = player.getHeldItemMainhand();
      if (stack == null) {
        return null;
      }

      // Get Enchantment List
      @SuppressWarnings("unchecked")
      List<String> validEnchantmentNames = new ArrayList<String>();
      Map<String, Enchantment> validEnchantments = new HashMap<String, Enchantment>();
      for (Enchantment enchantment : Enchantment.REGISTRY) {
        if (enchantment != null) {
          String name = I18n.translateToLocal(enchantment.getName()).replaceAll(" ", "");
          validEnchantmentNames.add(name);
        }
      }

      String[] validEnchantmentArray = new String[validEnchantmentNames.size()];
      validEnchantmentArray = validEnchantmentNames.toArray(validEnchantmentArray);
      return CommandBase.getListOfStringsMatchingLastWord(typedValue, validEnchantmentArray);
    }

    return null;
  }
  @SuppressWarnings("Duplicates")
  @Override
  public void execute(MinecraftServer server, ICommandSender iCommandSender, String[] args)
      throws CommandException {
    if (args.length == 0) {
      ChatHelper.sendMessage(
          iCommandSender, TextFormatting.RED + "Usage: /enchantforce <enchantmentname> [level]");
      return;
    }

    if (!PlayerMPUtil.isPlayer(iCommandSender)) {
      ChatHelper.sendMessage(iCommandSender, "Cannot enchant an item for CONSOLE");
      return;
    }

    String enchantstring = args[0];

    EntityPlayerMP player = (EntityPlayerMP) PlayerMPUtil.castToPlayer(iCommandSender);
    if (player == null) {
      ChatHelper.sendMessage(
          iCommandSender, "Cannot enchant an item for " + iCommandSender.getName());
      return;
    }

    ItemStack stack = player.getHeldItemMainhand();
    if (stack == null) {
      ChatHelper.sendMessage(iCommandSender, TextFormatting.RED + "Invalid Item held");
      return;
    }

    // Get Enchantment List
    @SuppressWarnings("unchecked")
    List<String> validEnchantmentNames = new ArrayList<String>();
    Map<String, Enchantment> validEnchantments = new HashMap<String, Enchantment>();
    for (Enchantment enchantment : Enchantment.REGISTRY) {
      if (enchantment != null) {
        String name = I18n.translateToLocal(enchantment.getName()).replaceAll(" ", "");
        validEnchantmentNames.add(name);
        validEnchantments.put(name.toLowerCase(), enchantment);
      }
    }

    @SuppressWarnings("unchecked")
    Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(stack);
    Enchantment enchantment = validEnchantments.get(enchantstring.toLowerCase());
    if (enchantment == null) {
      ChatHelper.sendMessage(
          iCommandSender, TextFormatting.RED + "Invalid enchantment: " + enchantstring);
      return;
    }

    int enchantlevel = enchantment.getMaxLevel();
    if (args.length == 2) {
      enchantlevel = CommandBase.parseInt(args[1]);
    }

    enchantments.put(enchantment, enchantlevel);

    EnchantmentHelper.setEnchantments(enchantments, stack);

    ChatHelper.sendMessage(
        iCommandSender,
        TextFormatting.GOLD
            + "Enchanted "
            + stack.getDisplayName()
            + " with "
            + TextFormatting.AQUA
            + enchantstring
            + TextFormatting.GOLD
            + " ("
            + TextFormatting.LIGHT_PURPLE
            + enchantlevel
            + TextFormatting.GOLD
            + ")");
    ChatHelper.sendAdminMessage(
        iCommandSender,
        "Enchanted "
            + stack.getDisplayName()
            + " with "
            + enchantstring
            + " ("
            + enchantlevel
            + ")");
  }
Пример #3
0
  @SubscribeEvent(priority = EventPriority.HIGHEST)
  public void onPlayerInteract(PlayerInteractEvent.RightClickBlock event) {

    World world = event.getWorld();
    BlockPos pos = event.getPos();

    if (world.isRemote) return;

    TileEntity tileEntity = world.getTileEntity(pos);
    if (tileEntity instanceof TileEntityLockable) {
      TileEntityLockable tileEntityLockable = (TileEntityLockable) tileEntity;

      EntityPlayerMP player = (EntityPlayerMP) event.getEntityPlayer();

      ItemStack current = player.getHeldItemMainhand();
      if (tileEntityLockable.isLocked()) {
        if (current != null && current.getItem() == ZeiyoItems.key) {
          String unlockPass = new StringBuilder(current.getDisplayName()).reverse().toString();

          if (tileEntityLockable.getLockCode().getLock().equals(unlockPass)) {
            tileEntityLockable.setLockCode(new LockCode(""));
            world.playSound(
                (EntityPlayer) null,
                player.posX,
                player.posY,
                player.posZ,
                SoundEvents.UI_BUTTON_CLICK,
                SoundCategory.BLOCKS,
                0.5F,
                world.rand.nextFloat() * 0.1F + 0.9F);
            player.addChatComponentMessage(
                new TextComponentTranslation("text.key.successOnUnlock", new Object[0]), true);

          } else if (!tileEntityLockable.getLockCode().getLock().equals(current.getDisplayName())) {
            world.playSound(
                (EntityPlayer) null,
                player.posX,
                player.posY,
                player.posZ,
                SoundEvents.UI_BUTTON_CLICK,
                SoundCategory.BLOCKS,
                0.5F,
                world.rand.nextFloat() * 0.1F + 0.9F);
            player.addChatComponentMessage(
                new TextComponentTranslation("text.key.errorOnKey", new Object[0]), true);
            event.setCanceled(true);
          }
        } else {
          world.playSound(
              (EntityPlayer) null,
              player.posX,
              player.posY,
              player.posZ,
              SoundEvents.UI_BUTTON_CLICK,
              SoundCategory.BLOCKS,
              0.5F,
              world.rand.nextFloat() * 0.1F + 0.9F);
          player.addChatComponentMessage(
              new TextComponentTranslation("text.key.errorOnBlock", new Object[0]), true);
          event.setCanceled(true);
        }
      } else {
        if (current != null && current.getItem() == ZeiyoItems.key) {
          if (!current
              .getDisplayName()
              .equals(I18n.translateToLocal(current.getItem().getUnlocalizedName() + ".name"))) {
            tileEntityLockable.setLockCode(new LockCode(current.getDisplayName()));
            world.playSound(
                (EntityPlayer) null,
                player.posX,
                player.posY,
                player.posZ,
                SoundEvents.UI_BUTTON_CLICK,
                SoundCategory.BLOCKS,
                0.5F,
                world.rand.nextFloat() * 0.1F + 0.9F);
            player.addChatComponentMessage(
                new TextComponentTranslation("text.key.successOnLock", new Object[0]), true);

          } else {

            player.addChatComponentMessage(
                new TextComponentTranslation("text.key.errorOnKeyName", new Object[0]), true);
          }
          event.setCanceled(true);
        }
      }
    }
  }