Refactor code to be more efficient.

This commit is contained in:
feildmaster 2012-03-25 18:06:43 -05:00 committed by Warren Loo
parent c30e339af6
commit 80db22b643
9 changed files with 25 additions and 30 deletions

View File

@ -164,7 +164,7 @@ public class Block {
public final Material material;
public float frictionFactor;
private String name;
public ArrayList<ItemStack> dropList = new ArrayList<ItemStack>(1); // CraftBukkit
public final ArrayList<ItemStack> dropList = new ArrayList<ItemStack>(1); // CraftBukkit
protected Block(int i, Material material) {
this.bR = true;
@ -362,7 +362,7 @@ public class Block {
protected void a(World world, int i, int j, int k, ItemStack itemstack) {
// CraftBukkit start - the logic of this function is moved into finishDrop
// This is such a hackish change it's rediculous.
// This is such a hackish change it's ridiculous.
this.dropList.add(itemstack);
}
@ -550,6 +550,7 @@ public class Block {
this.dropList.clear();
}
// Blocks that have different drops in certain situations need to override this. IE: Sheers on BlockLeaves
public ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
// CraftBukkit end
if (this.h() && EnchantmentManager.hasSilkTouchEnchantment(entityhuman.inventory)) {
@ -563,7 +564,8 @@ public class Block {
this.dropNaturally(world, i, j, k, l, 1.0F, i1); // CraftBukkit
}
return this.dropList; // CraftBukkit
return this.dropList;
}
protected boolean h() {

View File

@ -1,6 +1,5 @@
package net.minecraft.server;
import java.util.ArrayList; // CraftBukkit
import java.util.Random;
public class BlockDeadBush extends BlockFlower {
@ -38,13 +37,13 @@ public class BlockDeadBush extends BlockFlower {
}
// CraftBukkit start - Calculate drops
public ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
public java.util.ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
if (!world.isStatic && entityhuman.U() != null && entityhuman.U().id == Item.SHEARS.id) {
this.a(world, i, j, k, new ItemStack(Block.DEAD_BUSH, 1, l));
return this.dropList;
} else {
return super.calculateDrops(world, entityhuman, i, j, k, l);
}
return this.dropList;
}
// CraftBukkit end
}

View File

@ -1,6 +1,5 @@
package net.minecraft.server;
import java.util.ArrayList; // CraftBukkit
import java.util.Random;
import org.bukkit.event.block.LeavesDecayEvent; // CraftBukkit
@ -173,13 +172,13 @@ public class BlockLeaves extends BlockTransparant {
}
// CraftBukkit start - Calculate drops
public ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
public java.util.ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
if (!world.isStatic && entityhuman.U() != null && entityhuman.U().id == Item.SHEARS.id) {
this.a(world, i, j, k, new ItemStack(Block.LEAVES.id, 1, l & 3));
return this.dropList;
} else {
return super.calculateDrops(world, entityhuman, i, j, k, l);
}
return this.dropList;
}
// CraftBukkit end

View File

@ -1,6 +1,5 @@
package net.minecraft.server;
import java.util.ArrayList; // CraftBukkit
import java.util.Random;
public class BlockLongGrass extends BlockFlower {
@ -38,13 +37,13 @@ public class BlockLongGrass extends BlockFlower {
}
// CraftBukkit start - Calculate drops
public ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
public java.util.ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
if (!world.isStatic && entityhuman.U() != null && entityhuman.U().id == Item.SHEARS.id) {
this.a(world, i, j, k, new ItemStack(Block.LONG_GRASS, 1, l));
return this.dropList;
} else {
return super.calculateDrops(world, entityhuman, i, j, k, l);
}
return this.dropList;
}
// CraftBukkit end
}

View File

@ -14,7 +14,7 @@ public class BlockPistonExtension extends Block {
}
// CraftBukkit start - Support getDrops() in BlockBreakEvent
public ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int d) {
public java.util.ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int d) {
super.calculateDrops(world, entityhuman, i, j, k, d);
int l = world.getData(i, j, k) & 0x7;
if (l > 5 || l < 0) return this.dropList;

View File

@ -1,6 +1,5 @@
package net.minecraft.server;
import java.util.ArrayList; // CraftBukkit
import java.util.Random;
import org.bukkit.craftbukkit.event.CraftEventFactory;
@ -74,7 +73,7 @@ public class BlockSnow extends Block {
}
// CraftBukkit start - Calculate drops
public ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
public java.util.ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
this.a(world, i, j, k, new ItemStack(Item.SNOW_BALL.id, 1, 1));
return this.dropList;
}

View File

@ -1,6 +1,5 @@
package net.minecraft.server;
import java.util.ArrayList; // CraftBukkit
import java.util.Random;
public class BlockTNT extends Block {
@ -57,7 +56,7 @@ public class BlockTNT extends Block {
}
// CraftBukkit start - Calculate drops
public ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
public java.util.ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
if ((l & 1) == 0) {
this.a(world, i, j, k, new ItemStack(Block.TNT.id, 1, 0));
}

View File

@ -1,6 +1,5 @@
package net.minecraft.server;
import java.util.ArrayList; // CraftBukkit
import java.util.Random;
public class BlockVine extends Block {
@ -298,13 +297,13 @@ public class BlockVine extends Block {
}
// CraftBukkit start - Calculate drops
public ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
public java.util.ArrayList<ItemStack> calculateDrops(World world, EntityHuman entityhuman, int i, int j, int k, int l) {
if (!world.isStatic && entityhuman.U() != null && entityhuman.U().id == Item.SHEARS.id) {
this.a(world, i, j, k, new ItemStack(Block.VINE, 1, 0));
return this.dropList;
} else {
return super.calculateDrops(world, entityhuman, i, j, k, l);
}
return this.dropList;
}
// CraftBukkit end
}

View File

@ -1,8 +1,6 @@
package org.bukkit.craftbukkit.event;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -18,7 +16,6 @@ import net.minecraft.server.EntityItem;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.EntityPotion;
import net.minecraft.server.IInventory;
import net.minecraft.server.InventoryCrafting;
import net.minecraft.server.Item;
import net.minecraft.server.ItemStack;
@ -512,12 +509,13 @@ public class CraftEventFactory {
return event;
}
private static final List<org.bukkit.inventory.ItemStack> drops = new java.util.ArrayList<org.bukkit.inventory.ItemStack>();
public static boolean callBlockBreakEvent(World world, int x, int y, int z, int id, int data, boolean creative, EntityHuman player) {
net.minecraft.server.Block blockType = net.minecraft.server.Block.byId[id];
if (blockType == null) { // Illegal block ID
return true;
}
Block block = world.getWorld().getBlockAt(x, y, z);
// Tell client the block is gone immediately then process events
if (world.getTileEntity(x, y, z) == null) {
@ -528,10 +526,12 @@ public class CraftEventFactory {
((EntityPlayer) player).netServerHandler.sendPacket(packet);
}
List<ItemStack> calculatedDrops = blockType.calculateDrops(world, player, x, y, z, data);
List<org.bukkit.inventory.ItemStack> drops = new ArrayList<org.bukkit.inventory.ItemStack>(calculatedDrops.size());
Block block = world.getWorld().getBlockAt(x, y, z);
List<ItemStack> toDrop = blockType.calculateDrops(world, player, x, y, z, data);
drops.clear();
if (!creative && player.b(blockType)) {
for (ItemStack stack : calculatedDrops) {
for (ItemStack stack : toDrop) {
drops.add(new CraftItemStack(stack));
}
}
@ -540,17 +540,16 @@ public class CraftEventFactory {
world.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
blockType.dropList.clear();
toDrop.clear();
// Let the client know the block still exists
((EntityPlayer) player).netServerHandler.sendPacket(new Packet53BlockChange(x, y, z, world));
return true;
}
ArrayList<ItemStack> toDrop = new ArrayList<ItemStack>(drops.size());
toDrop.clear();
for (org.bukkit.inventory.ItemStack stack : drops) {
toDrop.add(CraftItemStack.createNMSItemStack(stack));
}
blockType.dropList = toDrop;
return false; // Event not cancelled
}