查看: 313|回复: 0
打印 上一主题 下一主题

[求助] getRandomItem死循环问题

[复制链接]

1

主题

0

回帖

1

积分

Lv.1 新手木匠

UID
8756182
小麦
0
金锭
6
下界之星
0
跳转到指定楼层
楼主
发表于 2023-2-5 22:59:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式 IP:湖南
package gycgf.top.mcmod.item;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.Random;

public class ItemOperation {

    private ItemStack _return;

    public ItemOperation(@Nullable String dictionary){
        if(!Objects.equals(dictionary, "")) this._return = this.getRandomItem(dictionary);
    }

    /**
     * 说明:获取一个词典中的随机物品<p>
     * 参数:<p>
     *      dictionary : 物品词典<p>
     * 返回:物品堆栈 */
    public ItemStack getRandomItem(String dictionary){
        return this.getRandomItem(dictionary,Items.AIR);
    }

    /**
     * 说明:获取一个词典中的随机物品<p>
     * 参数:<p>
     *      dictionary : 物品词典<p>
     *      item_name : 缺省值,当 dictionary 物品词典中不存在物品时返回<p>
     * 返回:物品堆栈 */
    private ItemStack getRandomItem(@NotNull String dictionary,Item item_name){
        ItemStack return_;

        return_ = new ItemStack(Objects.requireNonNull(ForgeRegistries.ITEMS.tags())
                .getTag(ItemTags.create(new ResourceLocation(dictionary)))
                .getRandomElement(new Random()).orElse(item_name));

        return return_;
    }

    /**
     * 说明:获取构造方法创建的物品堆栈<p>
     * 返回:物品堆栈 */
    public ItemStack get_return(){
        return this._return;
    }
}

在执行到上述getRandomItem(@NotNull String dictionary,Item item_name)方法时,游戏卡死。(大概是进了某个死循环了)但是在其它启动器上没问题
不知道是不是ResourceLocation方法和ItemTags方法的问题
望大佬指点不开窍的我
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

返回顶部