Files
src
build.gradle
gradle.properties
settings.gradle
Code
11 355
src/main/java/com/ngs/ngsluckytrees/NgsLuckyTrees.java
package com.ngs.ngsluckytrees;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LeavesBlock;
import net.minecraft.world.level.block.sounds.AmbientLeavesBlockSoundPlayer;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.item.BlockItem;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
public final class NgsLuckyTrees implements ModInitializer {
public static final String MOD_ID = "ngsluckytrees";
private static final Identifier LUCKY_LEAVES_ID = Identifier.fromNamespaceAndPath(MOD_ID, "lucky_leaves");
private static final ResourceKey<Item> LUCKY_LEAVES_ITEM_KEY = ResourceKey.create(Registries.ITEM, LUCKY_LEAVES_ID);
public static final Block LUCKY_LEAVES = Registry.register(
BuiltInRegistries.BLOCK,
LUCKY_LEAVES_ID,
new LuckyLeavesBlock(BlockBehaviour.Properties.ofFullCopy(Blocks.OAK_LEAVES))
);
public static final Item LUCKY_LEAVES_ITEM = Registry.register(
BuiltInRegistries.ITEM,
LUCKY_LEAVES_ITEM_KEY,
new BlockItem(LUCKY_LEAVES, new Item.Properties().setId(LUCKY_LEAVES_ITEM_KEY))
);
private static int jackpotChance = 1;
private static int mobChance = 10;
private static int eventChance = 15;
@Override
public void onInitialize() {
loadConfig();
ResourceKey<CreativeModeTab> naturalBlocks = ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.withDefaultNamespace("natural_blocks")
);
CreativeModeTabEvents.modifyOutputEvent(naturalBlocks).register(output -> output.accept(LUCKY_LEAVES_ITEM));
ServerChunkEvents.CHUNK_LOAD.register(NgsLuckyTrees::convertLeaves);
PlayerBlockBreakEvents.AFTER.register((level, player, pos, state, blockEntity) -> {
if (state.getBlock() != LUCKY_LEAVES || !(level instanceof ServerLevel serverLevel)) {
return;
}
ThreadLocalRandom random = ThreadLocalRandom.current();
if (random.nextInt(100) < jackpotChance) {
jackpot(serverLevel, pos, random);
} else if (random.nextInt(100) < eventChance) {
surpriseEvent(serverLevel, pos, random);
} else if (random.nextInt(100) < mobChance) {
mobSurprise(serverLevel, pos, random);
} else {
ordinaryReward(serverLevel, pos, random);
}
});
}
private static void convertLeaves(ServerLevel level, LevelChunk chunk, boolean generated) {
if (!level.dimension().equals(Level.OVERWORLD)) {
return;
}
ChunkPos chunkPos = chunk.getPos();
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
for (int x = chunkPos.getMinBlockX(); x <= chunkPos.getMaxBlockX(); x++) {
for (int z = chunkPos.getMinBlockZ(); z <= chunkPos.getMaxBlockZ(); z++) {
for (int y = level.getMinY(); y <= level.getMaxY(); y++) {
pos.set(x, y, z);
BlockState state = chunk.getBlockState(pos);
if (state.getBlock() instanceof LeavesBlock && state.getBlock() != LUCKY_LEAVES) {
level.setBlock(pos, LUCKY_LEAVES.defaultBlockState(), 3, 512);
}
}
}
}
}
private static void loadConfig() {
Path path = Path.of("config", MOD_ID + ".properties");
Properties properties = new Properties();
properties.setProperty("jackpot_chance_percent", "1");
properties.setProperty("mob_surprise_chance_percent", "10");
properties.setProperty("event_surprise_chance_percent", "15");
try {
Files.createDirectories(path.getParent());
if (Files.exists(path)) {
try (var reader = Files.newBufferedReader(path)) {
properties.load(reader);
}
} else {
try (var writer = Files.newBufferedWriter(path)) {
properties.store(writer, "NGS Lucky Trees chances (0-100 percent)");
}
}
} catch (IOException exception) {
throw new UncheckedIOException("Could not read or create " + path, exception);
}
jackpotChance = readChance(properties, "jackpot_chance_percent", 1);
mobChance = readChance(properties, "mob_surprise_chance_percent", 10);
eventChance = readChance(properties, "event_surprise_chance_percent", 15);
}
private static int readChance(Properties properties, String key, int fallback) {
try {
return Math.max(0, Math.min(100, Integer.parseInt(properties.getProperty(key))));
} catch (NumberFormatException exception) {
return fallback;
}
}
private static void ordinaryReward(ServerLevel level, BlockPos pos, ThreadLocalRandom random) {
if (random.nextInt(1000) == 0) {
drop(level, pos, new ItemStack(Items.ENCHANTED_GOLDEN_APPLE));
return;
}
if (random.nextInt(200) == 0) {
drop(level, pos, new ItemStack(Items.NETHERITE_INGOT));
return;
}
ItemStack reward = switch (random.nextInt(12)) {
case 0 -> new ItemStack(Items.DIAMOND, random.nextInt(1, 5));
case 1 -> new ItemStack(Items.EMERALD, random.nextInt(2, 7));
case 2 -> new ItemStack(Items.GOLD_INGOT, random.nextInt(3, 9));
case 3 -> new ItemStack(Items.IRON_INGOT, random.nextInt(4, 13));
case 4 -> new ItemStack(Items.GOLDEN_APPLE);
case 5 -> new ItemStack(Items.EXPERIENCE_BOTTLE, random.nextInt(2, 6));
case 6 -> new ItemStack(Items.BREAD, random.nextInt(2, 9));
case 7 -> new ItemStack(Items.COOKED_BEEF, random.nextInt(2, 7));
case 8 -> enchanted(level, Items.DIAMOND_SWORD, Enchantments.SHARPNESS, 2);
case 9 -> enchanted(level, Items.DIAMOND_HELMET, Enchantments.PROTECTION, 2);
case 10 -> enchanted(level, Items.BOW, Enchantments.POWER, 2);
default -> enchanted(level, Items.DIAMOND_PICKAXE, Enchantments.EFFICIENCY, 2);
};
drop(level, pos, reward);
}
private static void jackpot(ServerLevel level, BlockPos pos, ThreadLocalRandom random) {
switch (random.nextInt(3)) {
case 0 -> drop(level, pos, new ItemStack(Items.DIAMOND, random.nextInt(32, 65)));
case 1 -> {
drop(level, pos, enchanted(level, Items.NETHERITE_SWORD, Enchantments.SHARPNESS, 4));
drop(level, pos, enchanted(level, Items.NETHERITE_HELMET, Enchantments.PROTECTION, 4));
}
default -> {
drop(level, pos, new ItemStack(Items.DIAMOND_BLOCK, 3));
drop(level, pos, new ItemStack(Items.EMERALD_BLOCK, 2));
drop(level, pos, new ItemStack(Items.NETHERITE_INGOT));
}
}
}
private static void mobSurprise(ServerLevel level, BlockPos pos, ThreadLocalRandom random) {
EntityType<?> type = switch (random.nextInt(5)) {
case 0 -> EntityTypes.CHICKEN;
case 1 -> EntityTypes.COW;
case 2 -> EntityTypes.IRON_GOLEM;
case 3 -> EntityTypes.ZOMBIE;
default -> EntityTypes.CREEPER;
};
spawn(level, pos, type);
}
private static void surpriseEvent(ServerLevel level, BlockPos pos, ThreadLocalRandom random) {