网易我的世界论坛
标题:
getRandomItem死循环问题
[打印本页]
作者:
gyc316
时间:
2023-2-5 22:59
标题:
getRandomItem死循环问题
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
方法的问题
望大佬指点不开窍的我
欢迎光临 网易我的世界论坛 (http://mc.netease.com/)
Powered by Discuz! X3.3