Skocz do zawartości
  • 0

Dodawanie itemów do eq poprzez stringListe


Pytanie

Mam oto taki kod: W komendzie /items

 

				
                      
	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		Player p = (Player) sender;
		if(command.getName().equalsIgnoreCase("items")){

			List<ItemStack> items = new ArrayList<ItemStack>();
                      
			for(String itemsx : FileManager.getConfig().getStringList("drops")){
				items.add(Utils.parseItemStack(itemsx));
			}
                      
			for(ItemStack is : items){
				p.getInventory().addItem(is);
			}
              
		}
	}

Klasa Utils:

 

	public static boolean isInt(String s){
		try{
			Integer.parseInt(s);
			return true;
		}
		catch (NumberFormatException ex) {}
		return false;
	}
	
	public static String replace(String text, String searchString, String replacement){
		if ((text == null) || (text.isEmpty()) || (searchString.isEmpty()) || (replacement == null)) {
			return text;
		}
		int start = 0;
		int max = -1;
		int end = text.indexOf(searchString, start);
		if (end == -1) {
			return text;
		}
		int replacedLength = searchString.length();
		int increase = replacement.length() - replacedLength;
		increase = increase < 0 ? 0 : increase;
		increase *= (max > 64 ? 64 : max < 0 ? 16 : max);
		StringBuilder sb = new StringBuilder(text.length() + increase);
		while (end != -1){
			sb.append(text.substring(start, end)).append(replacement);
			start = end + replacedLength;
			max--;
			if (max == 0) {
				break;
			}
			end = text.indexOf(searchString, start);
		}
		sb.append(text.substring(start));
		return sb.toString();
	}
	
	public static ItemStack parseItemStack(String itemStack){
		ItemStack is = new ItemStack(Material.AIR);
		String[] strings = itemStack.split(" ");
		String[] item = strings[0].split(":");
		if (item.length > 1){
			Material m = Material.getMaterial(Integer.parseInt(item[0]));
			is.setType(m);
			is.setDurability(Short.parseShort(item[1]));
		}else if (isInt(item[0])){
			Material m = Material.getMaterial(Integer.parseInt(item[0]));
			is.setType(m);
		}else{
			Material m = Material.getMaterial(item[0]);
			is.setType(m);
		}
		int amount = 1;
		if (isInt(strings[1])) {
			amount = Integer.parseInt(strings[1]);
		}
		is.setAmount(amount);
		for (int i = 2; i < strings.length; i++){
			String s = strings[i];
			String[] trim = s.split(":");
			if (trim.length >= 1) {
				if (trim[0].equalsIgnoreCase("name")){
					ItemMeta im = is.getItemMeta();
					String name = c(replace(trim[1], "_", " "));
					im.setDisplayName(name);
					is.setItemMeta(im);
				}else if (trim[0].equalsIgnoreCase("lore")){
					ItemMeta im = is.getItemMeta();
					trim[1] = replace(trim[1], "_", " ");
					String[] lorestring = trim[1].split("&nl");
					List<String> lore = new ArrayList<String>();
					for (String s1 : lorestring) {
						lore.add(c(s1));
					}
					im.setLore(lore);
					is.setItemMeta(im);
				}else if (trim[0].equalsIgnoreCase("x")){
				}else{
					Enchantment e = Enchantments.getByName(trim[0]);
					if (e != null){
						int lvl = Integer.parseInt(trim[1]);
						ItemMeta im = is.getItemMeta();
						is.setItemMeta(im);
						is.addUnsafeEnchantment(e, lvl);
					}
				}
			}
		}
		return is;	
	}
          
    public static String c (String s){
		return ChatColor.translateAlternateColorCodes('&', s);
	}

Plik config.yml

drops:
  - "1 name:&6xd"
  - "1 name:&6aa"

 

Odnośnik do komentarza
https://skript.pl/temat/23196-dodawanie-item%C3%B3w-do-eq-poprzez-stringliste/
Udostępnij na innych stronach

4 odpowiedzi na to pytanie

Rekomendowane odpowiedzi

Nieaktywny
Ten temat został zamknięty. Brak możliwości dodania odpowiedzi.
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...