diff --git a/src/main/java/me/ellieis/Sabotage/game/custom/SabotageBlocks.java b/src/main/java/me/ellieis/Sabotage/game/custom/SabotageBlocks.java index d3d26c6..983f506 100644 --- a/src/main/java/me/ellieis/Sabotage/game/custom/SabotageBlocks.java +++ b/src/main/java/me/ellieis/Sabotage/game/custom/SabotageBlocks.java @@ -4,23 +4,14 @@ import me.ellieis.Sabotage.Sabotage; import me.ellieis.Sabotage.game.custom.blocks.SabotageChest; import me.ellieis.Sabotage.game.custom.blocks.SabotageChestBlockEntity; -import me.ellieis.Sabotage.game.phase.SabotageActive; -import net.fabricmc.fabric.api.event.player.UseBlockCallback; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; import net.minecraft.block.Blocks; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; -import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvents; -import net.minecraft.util.ActionResult; -import net.minecraft.util.Hand; -import net.minecraft.util.hit.BlockHitResult; -import net.minecraft.world.World; public class SabotageBlocks { @@ -32,30 +23,6 @@ public static void register() { registerBlockEntity("sabotage_chest_block_entity", SABOTAGE_CHEST_ENTITY); - UseBlockCallback.EVENT.register(SabotageBlocks::onBlockUse); - } - - private static ActionResult onBlockUse(PlayerEntity plr, World world, Hand hand, BlockHitResult blockHitResult) { - boolean isInGame = false; - - for (SabotageActive game : Sabotage.activeGames) { - if (game.getWorld().equals(world)) { - isInGame = true; - break; - } - } - - if (blockHitResult != null) { - Block block = world.getBlockState(blockHitResult.getBlockPos()).getBlock(); - if (block instanceof SabotageChest) { - if (isInGame) { - plr.playSound(SoundEvents.BLOCK_CHEST_CLOSE, SoundCategory.BLOCKS, 1, 1.2f); - world.setBlockState(blockHitResult.getBlockPos(), Blocks.AIR.getDefaultState()); - } - return ActionResult.FAIL; - } - } - return ActionResult.PASS; } private static T register(String id, T block) { diff --git a/src/main/java/me/ellieis/Sabotage/game/custom/blocks/SabotageChest.java b/src/main/java/me/ellieis/Sabotage/game/custom/blocks/SabotageChest.java index ab9f516..42ea523 100644 --- a/src/main/java/me/ellieis/Sabotage/game/custom/blocks/SabotageChest.java +++ b/src/main/java/me/ellieis/Sabotage/game/custom/blocks/SabotageChest.java @@ -1,18 +1,75 @@ package me.ellieis.Sabotage.game.custom.blocks; +import com.google.common.util.concurrent.AtomicDouble; import eu.pb4.polymer.core.api.block.PolymerBlock; -import net.minecraft.block.Block; -import net.minecraft.block.BlockEntityProvider; -import net.minecraft.block.BlockState; -import net.minecraft.block.ChestBlock; +import me.ellieis.Sabotage.Sabotage; +import me.ellieis.Sabotage.game.phase.SabotageActive; +import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvents; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; import static me.ellieis.Sabotage.game.custom.SabotageBlocks.SABOTAGE_CHEST_ENTITY; +import static java.util.Map.entry; public class SabotageChest extends ChestBlock implements BlockEntityProvider, PolymerBlock { private final Block virtualBlock; - + private static final Map items = Map.ofEntries( + entry(Items.WOODEN_SWORD, 40), + entry(Items.STONE_SWORD, 15), + entry(Items.IRON_SWORD, 5), + entry(Items.WOODEN_AXE, 5), + entry(Items.GOLDEN_AXE, 1), + entry(Items.LEATHER_BOOTS, 20), + entry(Items.LEATHER_HELMET, 20), + entry(Items.LEATHER_LEGGINGS, 12), + entry(Items.LEATHER_CHESTPLATE, 10), + entry(Items.CHAINMAIL_BOOTS, 8), + entry(Items.CHAINMAIL_HELMET, 8), + entry(Items.CHAINMAIL_LEGGINGS, 5), + entry(Items.CHAINMAIL_CHESTPLATE, 3), + entry(Items.IRON_HELMET, 5), + entry(Items.IRON_BOOTS, 5), + entry(Items.IRON_LEGGINGS, 3), + entry(Items.IRON_CHESTPLATE, 1), + entry(Items.GOLDEN_APPLE, 1), + entry(Items.BOW, 4), + entry(Items.CROSSBOW, 4), + entry(Items.FIREWORK_ROCKET, 10), + entry(Items.ARROW, 20) + ); + @FunctionalInterface + private interface ThreadLocalRandomWrapper { + ThreadLocalRandom current(); + } + // I stole this from stackoverflow, don't ask me how it works + private static T getFromWeightedMap(Map weights) { + ThreadLocalRandomWrapper THREAD_LOCAL = ThreadLocalRandom::current; + if (weights == null || weights.isEmpty()) { + return null; + } + double chance = THREAD_LOCAL.current().nextDouble() * weights.values().stream().map(Number::doubleValue).reduce(0D, Double::sum); + AtomicDouble needle = new AtomicDouble(); + return weights.entrySet().stream().filter((ent) -> { + return needle.addAndGet(ent.getValue().doubleValue()) >= chance; + }).findFirst().map(Map.Entry::getKey).orElse(null); + } + private static ItemStack getItemDrop() { + Item item = getFromWeightedMap(items); + return new ItemStack(item); + } public SabotageChest(Settings settings, Block virtualBlock) { super(settings, () -> SABOTAGE_CHEST_ENTITY); @@ -28,6 +85,24 @@ public Block getPolymerBlock() { return this.virtualBlock; } + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity plr, Hand hand, BlockHitResult hit) { + boolean isInGame = false; + + for (SabotageActive game : Sabotage.activeGames) { + if (game.getWorld().equals(world)) { + isInGame = true; + break; + } + } + + if (isInGame) { + plr.playSound(SoundEvents.BLOCK_CHEST_CLOSE, SoundCategory.BLOCKS, 1, 1.2f); + plr.getInventory().insertStack(getItemDrop()); + world.setBlockState(pos, Blocks.AIR.getDefaultState()); + } + return ActionResult.FAIL; + } @Override public BlockState getPolymerBlockState(BlockState state) { return this.virtualBlock.getStateWithProperties(state);