Skip to content

Commit

Permalink
Add more lucky events
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugman76 committed Dec 4, 2024
1 parent 52c663c commit e8d37cd
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.hugman.lucky_block.lucky_event;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.LootTable;
import net.minecraft.loot.context.LootContextParameters;
import net.minecraft.loot.context.LootContextTypes;
import net.minecraft.loot.context.LootWorldContext;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* Lucky event that drops the content of a loot table.
*
* @author Hugman
* @since 1.0.0
*/
public record LootLuckyEvent(RegistryKey<LootTable> lootTable) implements LuckyEvent {
public static final MapCodec<LootLuckyEvent> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
RegistryKey.createCodec(RegistryKeys.LOOT_TABLE).fieldOf("loot_table").forGetter(LootLuckyEvent::lootTable)
).apply(instance, LootLuckyEvent::new));

public void trigger(ServerWorld world, PlayerEntity player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity) {
getLoots(world, pos, player, player.getMainHandStack()).forEach((stack) -> Block.dropStack(world, pos, stack));
}

public List<ItemStack> getLoots(ServerWorld world, BlockPos pos, @Nullable Entity entity, ItemStack stack) {
return world.getServer().getReloadableRegistries().getLootTable(this.lootTable)
.generateLoot(new LootWorldContext.Builder(world)
.add(LootContextParameters.ORIGIN, Vec3d.ofCenter(pos))
.add(LootContextParameters.TOOL, stack)
.add(LootContextParameters.BLOCK_STATE, world.getBlockState(pos))
.addOptional(LootContextParameters.BLOCK_ENTITY, world.getBlockEntity(pos))
.addOptional(LootContextParameters.THIS_ENTITY, entity)
.build(LootContextTypes.BLOCK));
}

@Override
public LuckyEventType<?> getType() {
return LuckyEventTypes.LOOT;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public class LuckyEventTypes {
public static final LuckyEventType<SummonEntityLuckyEvent> SUMMON_ENTITY = register("summon_entity", SummonEntityLuckyEvent.CODEC);
public static final LuckyEventType<LootLuckyEvent> LOOT = register("loot", LootLuckyEvent.CODEC);

private static <T extends LuckyEvent> LuckyEventType<T> register(String name, MapCodec<T> codec) {
return register(LuckyBlockMod.id(name), codec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public record SummonEntityLuckyEvent(NbtComponent data) implements LuckyEvent {
public static final MapCodec<SummonEntityLuckyEvent> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
NbtComponent.CODEC.optionalFieldOf("data", NbtComponent.DEFAULT).forGetter(SummonEntityLuckyEvent::data)
NbtComponent.CODEC_WITH_ID.optionalFieldOf("data", NbtComponent.DEFAULT).forGetter(SummonEntityLuckyEvent::data)
).apply(instance, SummonEntityLuckyEvent::new));

public void trigger(ServerWorld world, PlayerEntity player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/resources/data/lucky_block/loot_table/lucky_sword.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"type": "minecraft:block",
"pools": [
{
"bonus_rolls": 0,
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
],
"entries": [
{
"type": "minecraft:item",
"name": "minecraft:golden_sword",
"expand": true,
"functions": [
{
"function": "minecraft:enchant_randomly",
"only_compatible": true
},
{
"function": "minecraft:set_name",
"name": "Lucky Sword"
}
],
"conditions": []
}
],
"rolls": 1
}
],
"random_sequence": "lucky_block:lucky_item"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "lucky_block:loot",
"loot_table": "lucky_block:lucky_sword"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"type": "lucky_block:summon_entity",
"data": {
"id": "minecraft:zombie",
"CustomName": "Bob",
"ArmorItems": [
{
"id": "minecraft:diamond_boots",
"count": 1,
"components": {
"minecraft:enchantments": {
"minecraft:protection": 4,
"minecraft:unbreaking": 3
}
}
},
{
"id": "minecraft:diamond_leggings",
"count": 1,
"components": {
"minecraft:enchantments": {
"minecraft:protection": 4,
"minecraft:unbreaking": 3
}
}
},
{
"id": "minecraft:diamond_chestplate",
"count": 1,
"components": {
"minecraft:enchantments": {
"minecraft:protection": 4,
"minecraft:unbreaking": 3
}
}
},
{
"id": "minecraft:diamond_helmet",
"count": 1,
"components": {
"minecraft:enchantments": {
"minecraft:protection": 4,
"minecraft:unbreaking": 3
}
}
}
],
"HandItems": [
{
"id": "minecraft:diamond_sword",
"count": 1,
"components": {
"minecraft:enchantments": {
"minecraft:sharpness": 5,
"minecraft:unbreaking": 3,
"minecraft:fire_aspect": 2
}
}
},
{
"id": "minecraft:shield",
"count": 1,
"components": {
"minecraft:enchantments": {
"minecraft:unbreaking": 3
}
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "lucky_block:summon_entity",
"data": "{\"id\": \"minecraft:tnt\",\"fuse\": 20,\"Motion\":[0.0, 0.5, 0.0]}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "lucky_block:summon_entity",
"data": "{\"id\": \"minecraft:wind_charge\",\"Motion\":[0.0, -0.5, 0.0]}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"replace": false,
"values": [
"lucky_block:summon_creeper",
"lucky_block:summon_rainbow_sheep"
"lucky_block:summon_rainbow_sheep",
"lucky_block:summon_bob",
"lucky_block:summon_one_tnt",
"lucky_block:summon_one_wind_charge",
"lucky_block:loot_lucky_sword"
]
}

0 comments on commit e8d37cd

Please sign in to comment.