From 3dcd51c5d10e12e825a949b1894fbfac23c7b526 Mon Sep 17 00:00:00 2001 From: Darian Smith Date: Fri, 28 Jun 2024 00:03:29 -0400 Subject: [PATCH] Re-add getDrops and breakNaturally, now not broken (#92) * Add API to figure out block drops and break them * Revert NMS name changes in Block * Revert NMS name changes in EntityHuman * Revert NMS name changes in InventoryPlayer * Revert NMS name changes in Item * Revert NMS name changes in ItemInWorldManager * Revert NMS name changes in Material * Added comments to modified NMS code * Revert NMS name changes in ItemStack * Make Block.dropNaturally non-final again * Reverted the last of the name changes * Fixed grass chunk ban --- src/main/java/net/minecraft/server/Block.java | 22 ++++++-- .../java/net/minecraft/server/BlockBed.java | 10 +++- .../java/net/minecraft/server/BlockCrops.java | 24 ++++---- .../net/minecraft/server/BlockJukeBox.java | 6 -- .../minecraft/server/BlockPistonMoving.java | 14 +++-- .../net/minecraft/server/BlockStairs.java | 6 +- src/main/java/org/bukkit/block/Block.java | 29 ++++++++++ .../bukkit/craftbukkit/block/CraftBlock.java | 55 +++++++++++++++++++ .../craftbukkit/inventory/CraftItemStack.java | 4 ++ 9 files changed, 135 insertions(+), 35 deletions(-) diff --git a/src/main/java/net/minecraft/server/Block.java b/src/main/java/net/minecraft/server/Block.java index 80d873a2..9da3f340 100644 --- a/src/main/java/net/minecraft/server/Block.java +++ b/src/main/java/net/minecraft/server/Block.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.Random; public class Block { @@ -308,16 +309,29 @@ public void dropNaturally(World world, int i, int j, int k, int l, float f) { for (int j1 = 0; j1 < i1; ++j1) { // CraftBukkit - <= to < to allow for plugins to completely disable block drops from explosions if (world.random.nextFloat() < f) { - int k1 = this.a(l, world.random); - - if (k1 > 0) { - this.a(world, i, j, k, new ItemStack(k1, 1, this.a_(l))); + //Project Poseidon Start - New way to handle block drops to allow for plugins to know what items a block will drop + Optional> items = getDrops(world, i, j, k, l); + if(items.isPresent()) { + for(ItemStack item : items.get()) { + this.a(world, i, j, k, item); + } } + //Project Poseidon End } } } } + //Project Poseidon - API to get the drops of a block + public Optional> getDrops(World world, int x, int y, int z, int data){ + int id = this.a(data, world.random); + if(id <= 0){ + return Optional.empty(); + }else{ + return Optional.of(Arrays.asList(new ItemStack(id, 1, this.a_(data)))); + } + } + protected void a(World world, int i, int j, int k, ItemStack itemstack) { if (!world.isStatic) { float f = 0.7F; diff --git a/src/main/java/net/minecraft/server/BlockBed.java b/src/main/java/net/minecraft/server/BlockBed.java index 236e4bf0..434c7468 100644 --- a/src/main/java/net/minecraft/server/BlockBed.java +++ b/src/main/java/net/minecraft/server/BlockBed.java @@ -2,7 +2,10 @@ import org.bukkit.event.entity.EntityDamageEvent; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; +import java.util.Optional; import java.util.Random; public class BlockBed extends Block { @@ -189,10 +192,11 @@ public static ChunkCoordinates f(World world, int i, int j, int k, int l) { return null; } - public void dropNaturally(World world, int i, int j, int k, int l, float f) { - if (!d(l)) { - super.dropNaturally(world, i, j, k, l, f); + public Optional> getDrops(World world, int x, int y, int z, int data){ + if(!d(data)){ + return super.getDrops(world, x, y, z, data); } + return Optional.empty(); } public int e() { diff --git a/src/main/java/net/minecraft/server/BlockCrops.java b/src/main/java/net/minecraft/server/BlockCrops.java index 0025bc3d..8ae34e7c 100644 --- a/src/main/java/net/minecraft/server/BlockCrops.java +++ b/src/main/java/net/minecraft/server/BlockCrops.java @@ -1,5 +1,8 @@ package net.minecraft.server; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import java.util.Random; public class BlockCrops extends BlockFlower { @@ -86,22 +89,15 @@ public int a(int i, int j) { return this.textureId + j; } - public void dropNaturally(World world, int i, int j, int k, int l, float f) { - super.dropNaturally(world, i, j, k, l, f); - if (!world.isStatic) { - for (int i1 = 0; i1 < 3; ++i1) { - if (world.random.nextInt(15) <= l) { - float f1 = 0.7F; - float f2 = world.random.nextFloat() * f1 + (1.0F - f1) * 0.5F; - float f3 = world.random.nextFloat() * f1 + (1.0F - f1) * 0.5F; - float f4 = world.random.nextFloat() * f1 + (1.0F - f1) * 0.5F; - EntityItem entityitem = new EntityItem(world, (double) ((float) i + f2), (double) ((float) j + f3), (double) ((float) k + f4), new ItemStack(Item.SEEDS)); - - entityitem.pickupDelay = 10; - world.addEntity(entityitem); - } + public Optional> getDrops(World world, int x, int y, int z, int data){ + Optional> ret = super.getDrops(world, x, y, z, data); + List stacks = ret.orElse(new ArrayList<>(3)); + for(int i = 0; i < 3; i++){ + if(world.random.nextInt(15) <= 1){ + stacks.add(new ItemStack(Item.SEEDS)); } } + return Optional.of(stacks); } public int a(int i, Random random) { diff --git a/src/main/java/net/minecraft/server/BlockJukeBox.java b/src/main/java/net/minecraft/server/BlockJukeBox.java index 67f857a4..9fff2cdb 100644 --- a/src/main/java/net/minecraft/server/BlockJukeBox.java +++ b/src/main/java/net/minecraft/server/BlockJukeBox.java @@ -58,12 +58,6 @@ public void remove(World world, int i, int j, int k) { super.remove(world, i, j, k); } - public void dropNaturally(World world, int i, int j, int k, int l, float f) { - if (!world.isStatic) { - super.dropNaturally(world, i, j, k, l, f); - } - } - protected TileEntity a_() { return new TileEntityRecordPlayer(); } diff --git a/src/main/java/net/minecraft/server/BlockPistonMoving.java b/src/main/java/net/minecraft/server/BlockPistonMoving.java index 03d9ca29..9bdc25e3 100644 --- a/src/main/java/net/minecraft/server/BlockPistonMoving.java +++ b/src/main/java/net/minecraft/server/BlockPistonMoving.java @@ -1,5 +1,7 @@ package net.minecraft.server; +import java.util.List; +import java.util.Optional; import java.util.Random; public class BlockPistonMoving extends BlockContainer { @@ -54,13 +56,13 @@ public int a(int i, Random random) { return 0; } - public void dropNaturally(World world, int i, int j, int k, int l, float f) { - if (!world.isStatic) { - TileEntityPiston tileentitypiston = this.b(world, i, j, k); - if (tileentitypiston != null) { - Block.byId[tileentitypiston.a()].g(world, i, j, k, tileentitypiston.e()); - } + public Optional> getDrops(World world, int x, int y, int z, int data){ + TileEntityPiston tileentitypiston = this.b(world, x, y, z); + if(tileentitypiston != null){ + return Block.byId[tileentitypiston.a()].getDrops(world, x, y, z, tileentitypiston.e()); + }else{ + return Optional.empty(); } } diff --git a/src/main/java/net/minecraft/server/BlockStairs.java b/src/main/java/net/minecraft/server/BlockStairs.java index 67ca33cc..f38f773e 100644 --- a/src/main/java/net/minecraft/server/BlockStairs.java +++ b/src/main/java/net/minecraft/server/BlockStairs.java @@ -1,6 +1,8 @@ package net.minecraft.server; import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import java.util.Random; public class BlockStairs extends Block { @@ -117,8 +119,8 @@ public void remove(World world, int i, int j, int k) { this.a.remove(world, i, j, k); } - public void dropNaturally(World world, int i, int j, int k, int l, float f) { - this.a.dropNaturally(world, i, j, k, l, f); + public Optional> getDrops(World world, int x, int y, int z, int data){ + return this.a.getDrops(world, x, y, z, data); } public void b(World world, int i, int j, int k, Entity entity) { diff --git a/src/main/java/org/bukkit/block/Block.java b/src/main/java/org/bukkit/block/Block.java index 0cf844eb..3017eb90 100644 --- a/src/main/java/org/bukkit/block/Block.java +++ b/src/main/java/org/bukkit/block/Block.java @@ -1,9 +1,12 @@ package org.bukkit.block; +import java.util.Collection; + import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.inventory.ItemStack; /** * Represents a block. This is a live object, and only one Block may exist for @@ -277,4 +280,30 @@ public interface Block { * @return reaction */ PistonMoveReaction getPistonMoveReaction(); + + /** + * Returns a list of items which would drop by destroying this block + * @return a list of dropped items for this type of block + */ + Collection getDrops(); + + /** + * Returns a list of items which would drop by destroying this block with a specific tool + * @param tool The tool or item in hand used for digging + * @return a list of dropped items for this type of block + */ + Collection getDrops(ItemStack tool); + + /** + * Breaks the block and spawns items as if a player had digged it regardless of the tool + * @return true if the block was broken + */ + boolean breakNaturally(); + + /** + * Breaks the block and spawns items as if a player had digged it + * @param tool The tool or item in hand used for digging + * @return true if the block was broken + */ + boolean breakNaturally(ItemStack tool); } diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java index 419d84f6..c23dd013 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java @@ -2,13 +2,22 @@ import net.minecraft.server.BiomeBase; import net.minecraft.server.BlockRedstoneWire; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.*; import org.bukkit.craftbukkit.CraftChunk; +import org.bukkit.inventory.ItemStack; import org.bukkit.util.BlockVector; +import org.bukkit.craftbukkit.CraftWorld; +import org.bukkit.craftbukkit.inventory.CraftItemStack; public class CraftBlock implements Block { private final CraftChunk chunk; @@ -309,4 +318,50 @@ public PistonMoveReaction getPistonMoveReaction() { return PistonMoveReaction.getById(net.minecraft.server.Block.byId[this.getTypeId()].material.j()); } + + @Override + public Collection getDrops() { + net.minecraft.server.Block block = net.minecraft.server.Block.byId[getTypeId()]; + if (block == null) { + return new ArrayList(); + }else if(block.material.i()) { + return Arrays.asList(block.getDrops(((CraftWorld) this.getWorld()).getHandle(), x, y, z, (int)getData()).map((list) -> list.stream().map(itemStack -> new CraftItemStack(itemStack)).toArray(ItemStack[]::new)).orElse(new ItemStack[0])); + }else { + return new ArrayList(); + } + } + + @Override + public Collection getDrops(ItemStack tool) { + net.minecraft.server.Block block = net.minecraft.server.Block.byId[getTypeId()]; + if (block == null) { + return new ArrayList(); + }else if(block.material.i() || ((CraftItemStack)tool).getHandle().b(block)) { + return Arrays.asList(block.getDrops(((CraftWorld) this.getWorld()).getHandle(), x, y, z, (int)getData()).map((list) -> list.stream().map(itemStack -> new CraftItemStack(itemStack)).toArray(ItemStack[]::new)).orElse(new ItemStack[0])); + }else { + return new ArrayList(); + } + } + + @Override + public boolean breakNaturally() { + return breakNaturally(new ItemStack(Material.AIR)); + } + + @Override + public boolean breakNaturally(ItemStack tool) { + net.minecraft.server.Block block = net.minecraft.server.Block.byId[getTypeId()]; + if (block == null) { + return false; + }else{ + Collection drops = getDrops(tool); + boolean flag = setTypeId(0); + if (flag) { + for (ItemStack drop : drops) { + getWorld().dropItemNaturally(getLocation(), drop); + } + } + return flag; + } + } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index dcebeade..5e612993 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -123,4 +123,8 @@ public short getDurability() { public int getMaxStackSize() { return item.getItem().getMaxStackSize(); } + + public net.minecraft.server.ItemStack getHandle() { + return item; + } }