Вопрос Генерация предметов в сундуках

Destroy

Разработчик
Пользователь
Сообщения
777
Решения
30
Веб-сайт
destroydevs.ru
Java:
public class OpenChestListener implements Listener {

    HashMap<Location, Long> loc = new HashMap<>();

    private final int r = (int) (Math.random()*10);
    private final int slot = (int) (Math.random()*26);
    private final double d = Math.random()*100;

    private void openChest(Block block) {
        for(int i = 0; i<5; i++) {
            ItemStack item;
            if(d<10) {
                item = ItemsUtils.scrap(r);
            } else if(d<20) {
                item = ItemsUtils.antidot(1);
            } else if(d<50) {
                item = ItemsUtils.freshFood(r);
            } else {
                item = ItemsUtils.rottenFood(r);
            }

            Chest chest = (Chest) block.getState();
            chest.getBlockInventory().setItem(slot, item);
        }
        loc.put(block.getLocation(), System.currentTimeMillis());
    }

    @EventHandler
    public void onOpen(PlayerInteractEvent e) {
        if (e.getClickedBlock() == null) {
            return;
        }
        if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
            Block block = e.getClickedBlock();
            if(block.getType() != Material.CHEST) {
                return;
            }
            if(!loc.containsKey(block.getLocation())) {
                openChest(block);
            } else {
                long time = (System.currentTimeMillis() - loc.get(block.getLocation()));
                if (time >= 30) {
                    openChest(block);
                }
            }
        }
    }
}


Java:
    public static ItemStack scrap(int amount) {
        ItemStack item = new ItemStack(Material.BRICK, amount);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(ChatUtils.color("&7Хлам"));
        meta.setLore(Arrays.asList("", ChatUtils.color("&7Используется в крафтах.")));
        item.setItemMeta(meta);
        return item;
    }

    public static ItemStack antidot(int amount) {
        ItemStack item = new ItemStack(Material.HONEY_BOTTLE, amount);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(ChatUtils.color("&eАнти-дот"));
        meta.setLore(Arrays.asList("", ChatUtils.color("&eНажмите ПКМ, чтобы использовать предмет.")));
        item.setItemMeta(meta);
        return item;
    }

    public static ItemStack freshFood(int amount) {
        ItemStack item = new ItemStack(Material.COOKED_BEEF, amount);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(ChatUtils.color("&7Свежая еда"));
        meta.setLore(Arrays.asList("", ChatUtils.color("&7Вкусно пахнет...")));
        item.setItemMeta(meta);
        return item;
    }

    public static ItemStack rottenFood(int amount) {
        ItemStack item = new ItemStack(Material.COOKED_BEEF, amount);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(ChatUtils.color("&7Испорченная еда"));
        meta.setLore(Arrays.asList("", ChatUtils.color("&7Откуда эта вонь?")));
        item.setItemMeta(meta);
        return item;
    }

Генерируется только rottenFood:
Вам необходимо зарегистрироваться для просмотра изображений-вложений

Одинаково во всех сундуках.
Объединено

Java:
    private void openChest(Block block) {
        for(int i = 0; i<5; i++) {
            int r = (int) (Math.random()*10);
            int slot = (int) (Math.random()*26);
            double d = Math.random()*100;
            ItemStack item;
            if(d<10) {
                item = ItemsUtils.scrap(r);
            } else if(d<20) {
                item = ItemsUtils.antidot(1);
            } else if(d<50) {
                item = ItemsUtils.freshFood(r);
            } else {
                item = ItemsUtils.rottenFood(r);
            }

            Chest chest = (Chest) block.getState();
            chest.getBlockInventory().setItem(slot, item);
        }
        loc.put(block.getLocation(), System.currentTimeMillis());
    }
Понял косяк
 
Назад
Сверху Снизу