LeashedPlayer/src/main/java/com/r3944realms/leashedplayer/utils/Util.java
3944Realms db41f395f7 2024/11/11
優化了彩蛋物品的獲取方式
2024-11-11 17:40:36 +08:00

65 lines
2.7 KiB
Java

package com.r3944realms.leashedplayer.utils;
import com.r3944realms.leashedplayer.LeashedPlayer;
import com.r3944realms.leashedplayer.content.gamerules.Gamerules;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
import net.neoforged.fml.loading.FMLPaths;
import java.io.File;
import static com.r3944realms.leashedplayer.utils.Logger.logger;
public class Util {
public static String getGameruleName(Class<?> clazz) {
return Gamerules.GAMERULE_PREFIX + clazz.getSimpleName();
}
public static String getGameruleName(String gamerulesName) {
return Gamerules.GAMERULE_PREFIX + gamerulesName;
}
public static void configFileCreate(String[] children) {//初始化配置文件目录
File configFile = new File(FMLPaths.CONFIGDIR.get().toFile(), LeashedPlayer.MOD_ID);
if (!configFile.exists()) {
boolean mkdirSuccess = configFile.mkdirs();
if (!mkdirSuccess) {
logger.error("failed to create config directory for whimsicality");
throw new RuntimeException("failed to create config directory for " + LeashedPlayer.MOD_ID);
} else {
for (String child : children) {
File file = new File(configFile, child);
if (!file.exists()) {
boolean mkdirChildrenSuccess = file.mkdirs();
if (!mkdirChildrenSuccess) {
logger.error("failed to create " + child + " directory for +" + LeashedPlayer.MOD_ID);
throw new RuntimeException("failed to create " + child + " directory for" +LeashedPlayer.MOD_ID);
}
}
}
}
}
}
public static void throwItemTowardsLook(Entity thrower, Item itemToThrow, float velocity, float inaccuracy) {
ItemEntity thrownItem = new ItemEntity(
thrower.level(),
thrower.getX(),
thrower.getEyeY() - 0.1,
thrower.getZ(),
new ItemStack(itemToThrow)
);
thrownItem.setPickUpDelay(40);
Vec3 lookDirection = thrower.getLookAngle();
thrownItem.setDeltaMovement(
lookDirection.x * velocity + (thrower.level().random.nextGaussian() * 0.0075 * inaccuracy),
lookDirection.y * velocity + (thrower.level().random.nextGaussian() * 0.0075 * inaccuracy),
lookDirection.z * velocity + (thrower.level().random.nextGaussian() * 0.0075 * inaccuracy)
);
thrower.level().addFreshEntity(thrownItem);
}
}