Skip to content

Commit

Permalink
Re-add getDrops and breakNaturally, now not broken (retromcorg#92)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Meep3692 authored Jun 28, 2024
1 parent 8f68099 commit 3dcd51c
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 35 deletions.
22 changes: 18 additions & 4 deletions src/main/java/net/minecraft/server/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<List<ItemStack>> 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<List<ItemStack>> 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;
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/net/minecraft/server/BlockBed.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<List<ItemStack>> 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() {
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/net/minecraft/server/BlockCrops.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<List<ItemStack>> getDrops(World world, int x, int y, int z, int data){
Optional<List<ItemStack>> ret = super.getDrops(world, x, y, z, data);
List<ItemStack> 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) {
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/net/minecraft/server/BlockJukeBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/net/minecraft/server/BlockPistonMoving.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<List<ItemStack>> 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();
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/minecraft/server/BlockStairs.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<List<ItemStack>> 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) {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/bukkit/block/Block.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<ItemStack> 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<ItemStack> 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);
}
55 changes: 55 additions & 0 deletions src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -309,4 +318,50 @@ public PistonMoveReaction getPistonMoveReaction() {
return PistonMoveReaction.getById(net.minecraft.server.Block.byId[this.getTypeId()].material.j());

}

@Override
public Collection<ItemStack> getDrops() {
net.minecraft.server.Block block = net.minecraft.server.Block.byId[getTypeId()];
if (block == null) {
return new ArrayList<ItemStack>();
}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<ItemStack>();
}
}

@Override
public Collection<ItemStack> getDrops(ItemStack tool) {
net.minecraft.server.Block block = net.minecraft.server.Block.byId[getTypeId()];
if (block == null) {
return new ArrayList<ItemStack>();
}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<ItemStack>();
}
}

@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<ItemStack> drops = getDrops(tool);
boolean flag = setTypeId(0);
if (flag) {
for (ItemStack drop : drops) {
getWorld().dropItemNaturally(getLocation(), drop);
}
}
return flag;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@ public short getDurability() {
public int getMaxStackSize() {
return item.getItem().getMaxStackSize();
}

public net.minecraft.server.ItemStack getHandle() {
return item;
}
}

0 comments on commit 3dcd51c

Please sign in to comment.