Skip to content

Commit

Permalink
Replaced native entity usage in the item stacker
Browse files Browse the repository at this point in the history
  • Loading branch information
bergerkiller committed Dec 6, 2012
1 parent 2a60a63 commit b352956
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 68 deletions.
3 changes: 1 addition & 2 deletions src/com/bergerkiller/bukkit/nolagg/chunks/NoLaggChunks.java
Expand Up @@ -118,15 +118,14 @@ public void run() {
ChunkCoordinates chunkcoordinates = nmschunk.world.getSpawn();
int centerSpawnX = nmschunk.x * 16 + 8 - chunkcoordinates.x;
int centerSpawnZ = nmschunk.z * 16 + 8 - chunkcoordinates.z;
final short short1 = 128;
final int short1 = 128;
if (centerSpawnX >= -short1 && centerSpawnX <= short1 && centerSpawnZ >= -short1 && centerSpawnZ <= short1) {
continue;
}
}
if (!chunk.getWorld().isChunkInUse(chunk.getX(), chunk.getZ())) {
// Event
if (!CommonUtil.callEvent(new ChunkUnloadEvent(chunk)).isCancelled()) {
System.out.println("UNLOAD!");
unloadChunks.add(chunk);
}
}
Expand Down
16 changes: 5 additions & 11 deletions src/com/bergerkiller/bukkit/nolagg/itemstacker/NLISListener.java
@@ -1,7 +1,5 @@
package com.bergerkiller.bukkit.nolagg.itemstacker;

import net.minecraft.server.Entity;

import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand All @@ -10,35 +8,31 @@

import com.bergerkiller.bukkit.common.events.EntityAddEvent;
import com.bergerkiller.bukkit.common.events.EntityRemoveEvent;
import com.bergerkiller.bukkit.common.utils.EntityUtil;
import com.bergerkiller.bukkit.common.utils.WorldUtil;

public class NLISListener implements Listener {

@EventHandler(priority = EventPriority.MONITOR)
public void onWorldLoad(WorldLoadEvent event) {
StackFormer.get(WorldUtil.getNative(event.getWorld()));
StackFormer.get(event.getWorld());
}

@EventHandler(priority = EventPriority.MONITOR)
public void onWorldUnload(WorldUnloadEvent event) {
if (!event.isCancelled()) {
StackFormer.remove(WorldUtil.getNative(event.getWorld()));
StackFormer.remove(event.getWorld());
}
}

@EventHandler(priority = EventPriority.MONITOR)
public void onEntityAdd(EntityAddEvent event) {
Entity e = EntityUtil.getNative(event.getEntity());
if (e.dead) {
if (event.getEntity().isDead()) {
return;
}
StackFormer.get(e.world).addEntity(e);
StackFormer.get(event.getEntity().getWorld()).addEntity(event.getEntity());
}

@EventHandler(priority = EventPriority.MONITOR)
public void onEntityRemove(EntityRemoveEvent event) {
Entity e = EntityUtil.getNative(event.getEntity());
StackFormer.get(e.world).removeEntity(e);
StackFormer.get(event.getEntity().getWorld()).removeEntity(event.getEntity());
}
}
15 changes: 5 additions & 10 deletions src/com/bergerkiller/bukkit/nolagg/itemstacker/StackFormer.java
Expand Up @@ -6,12 +6,11 @@
import java.util.List;
import java.util.Map;

import net.minecraft.server.World;
import net.minecraft.server.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.World;

import com.bergerkiller.bukkit.common.AsyncTask;
import com.bergerkiller.bukkit.common.Task;
import com.bergerkiller.bukkit.common.utils.WorldUtil;
import com.bergerkiller.bukkit.nolagg.NoLagg;

public class StackFormer extends AsyncTask {
Expand All @@ -29,14 +28,10 @@ private StackFormer() {
}
}

public static WorldStackFormer get(org.bukkit.World world) {
return get(WorldUtil.getNative(world));
}

public static WorldStackFormer get(World world) {
WorldStackFormer former = globalWorlds.get(world);
if (former == null) {
former = new WorldStackFormer(world.getWorld());
former = new WorldStackFormer(world);
globalWorlds.put(world, former);
synchronized (former) {
toAdd.add(former);
Expand All @@ -45,7 +40,7 @@ public static WorldStackFormer get(World world) {
return former;
}

public static void remove(WorldServer world) {
public static void remove(World world) {
WorldStackFormer f = globalWorlds.remove(world);
if (f != null) {
f.disable();
Expand All @@ -62,7 +57,7 @@ public void run() {
}
}.start(0, NoLaggItemStacker.interval);

for (WorldServer world : WorldUtil.getWorlds()) {
for (World world : Bukkit.getWorlds()) {
get(world);
}
thread = new StackFormer().start(true);
Expand Down
40 changes: 25 additions & 15 deletions src/com/bergerkiller/bukkit/nolagg/itemstacker/StackingTask.java
Expand Up @@ -5,10 +5,12 @@
import java.util.Iterator;
import java.util.List;

import com.bergerkiller.bukkit.common.utils.ItemUtil;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;

import net.minecraft.server.Entity;
import net.minecraft.server.EntityItem;
import com.bergerkiller.bukkit.common.utils.EntityUtil;
import com.bergerkiller.bukkit.common.utils.ItemUtil;

public class StackingTask <T extends Entity> {
private T entity;
Expand Down Expand Up @@ -65,16 +67,16 @@ public boolean isValid() {
* @return True if processing is possible, False if not
*/
public boolean canProcess() {
return !this.entity.dead && this.nearby.size() >= NoLaggItemStacker.stackThreshold - 1;
return !this.entity.isDead() && this.nearby.size() >= NoLaggItemStacker.stackThreshold - 1;
}

/**
* Checks whether a given item reached it's maximum stacking size
*
* @return True if the item is maxed, False if not
*/
public static boolean isMaxed(EntityItem item) {
return item.itemStack.count >= ItemUtil.getMaxSize(item.itemStack);
public static boolean isMaxed(Item item) {
return item.getItemStack().getAmount() >= ItemUtil.getMaxSize(item.getItemStack());
}

/**
Expand All @@ -84,27 +86,29 @@ public static boolean isMaxed(EntityItem item) {
* @param radiusSquared for stacking
*/
public void fillNearby(List<StackingTask<T>> Entitytasks, final double radiusSquared) {
if (this.entity.dead) {
if (this.entity.isDead()) {
return;
}
T entity;
double d;
net.minecraft.server.Entity selfEntity = EntityUtil.getNative(this.entity);
for (StackingTask<T> task : Entitytasks) {
if (!task.isValid()) {
break; // Reached end of data
}
entity = task.entity;
if (!entity.dead && entity != this.entity) {
if (!entity.isDead() && entity != this.entity) {
// Distance check
d = distance(this.entity.locX, entity.locX);
net.minecraft.server.Entity e = EntityUtil.getNative(this.entity);
d = distance(selfEntity.locX, e.locX);
if (d > radiusSquared) {
continue;
}
d += distance(this.entity.locZ, entity.locZ);
d += distance(selfEntity.locZ, e.locZ);
if (d > radiusSquared) {
continue;
}
d += distance(this.entity.locY, entity.locY);
d += distance(selfEntity.locY, e.locY);
if (d > radiusSquared) {
continue;
}
Expand All @@ -115,17 +119,23 @@ public void fillNearby(List<StackingTask<T>> Entitytasks, final double radiusSqu
}

private void addNearby(T entity) {
if (this.entity instanceof EntityItem) {
if (this.entity instanceof Item) {
// Do a compatibility check
EntityItem from = (EntityItem) this.entity;
EntityItem to = (EntityItem) entity;
if (isMaxed(from) || from.itemStack.id != to.itemStack.id || from.itemStack.getData() != to.itemStack.getData()) {
Item from = (Item) this.entity;
Item to = (Item) entity;
if (isMaxed(from) || !itemEquals(from, to)) {
return;
}
}
this.nearby.add(entity);
}

private static boolean itemEquals(Item item1, Item item2) {
ItemStack stack1 = item1.getItemStack();
ItemStack stack2 = item2.getItemStack();
return stack1.getTypeId() == stack2.getTypeId() && stack1.getDurability() == stack2.getDurability();
}

private static double distance(final double d1, final double d2) {
return Math.abs((d1 - d2) * (d1 - d2));
}
Expand Down
Expand Up @@ -3,24 +3,24 @@
import java.util.LinkedList;

import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Item;

import com.bergerkiller.bukkit.common.utils.EntityUtil;
import com.bergerkiller.bukkit.common.utils.ItemUtil;

import net.minecraft.server.Entity;
import net.minecraft.server.EntityExperienceOrb;
import net.minecraft.server.EntityItem;

/**
* Handles the entity item and orb stacking process
*/
public class WorldStackFormer {
private boolean isProcessing = false; // Processing state, True for async busy or waiting, False for sync available
private final double radiusSquared;
private boolean disabled = false;
private final LinkedList<EntityItem> syncItems = new LinkedList<EntityItem>();
private final LinkedList<EntityExperienceOrb> syncOrbs = new LinkedList<EntityExperienceOrb>();
private LinkedList<StackingTask<EntityExperienceOrb>> orbTasks = new LinkedList<StackingTask<EntityExperienceOrb>>();
private LinkedList<StackingTask<EntityItem>> itemTasks = new LinkedList<StackingTask<EntityItem>>();
private final LinkedList<Item> syncItems = new LinkedList<Item>();
private final LinkedList<ExperienceOrb> syncOrbs = new LinkedList<ExperienceOrb>();
private LinkedList<StackingTask<ExperienceOrb>> orbTasks = new LinkedList<StackingTask<ExperienceOrb>>();
private LinkedList<StackingTask<Item>> itemTasks = new LinkedList<StackingTask<Item>>();

public WorldStackFormer(World world) {
this.radiusSquared = Math.pow(NoLaggItemStacker.stackRadius.get(world), 2.0);
Expand Down Expand Up @@ -48,12 +48,12 @@ public boolean isDisabled() {
* @param e the entity to add
*/
public void addEntity(Entity e) {
if (e instanceof EntityItem) {
if (e instanceof Item) {
if (!NoLaggItemStacker.isIgnoredItem(e)) {
syncItems.add((EntityItem) e);
syncItems.add((Item) e);
}
} else if (e instanceof EntityExperienceOrb && NoLaggItemStacker.stackOrbs) {
syncOrbs.add((EntityExperienceOrb) e);
} else if (e instanceof ExperienceOrb && NoLaggItemStacker.stackOrbs) {
syncOrbs.add((ExperienceOrb) e);
}
}

Expand All @@ -63,13 +63,13 @@ public void addEntity(Entity e) {
* @param e the entity to remove
*/
public void removeEntity(Entity e) {
if (e instanceof EntityItem) {
if (e instanceof Item) {
if (!NoLaggItemStacker.isIgnoredItem(e)) {
// don't bother doing an 'ignored item' check as it checks in a map or set anyway
syncItems.remove((EntityItem) e);
syncItems.remove((Item) e);
}
} else if (e instanceof EntityExperienceOrb && NoLaggItemStacker.stackOrbs) {
syncOrbs.remove((EntityExperienceOrb) e);
} else if (e instanceof ExperienceOrb && NoLaggItemStacker.stackOrbs) {
syncOrbs.remove((ExperienceOrb) e);
}
}

Expand All @@ -81,15 +81,15 @@ public void runAsync() {
return;
}
// Generate nearby items
for (StackingTask<EntityItem> task : itemTasks) {
for (StackingTask<Item> task : itemTasks) {
if (!task.isValid()) {
break; // Reached end of data
}
task.fillNearby(itemTasks, radiusSquared);
}
// Generate nearby orbs
if (NoLaggItemStacker.stackOrbs) {
for (StackingTask<EntityExperienceOrb> task : orbTasks) {
for (StackingTask<ExperienceOrb> task : orbTasks) {
if (!task.isValid()) {
break; // Reached end of data
}
Expand All @@ -110,44 +110,48 @@ public void runSync() {

// Finalize item stacking
boolean changed;
for (StackingTask<EntityItem> itemTask : itemTasks) {
for (StackingTask<Item> itemTask : itemTasks) {
if (!itemTask.isValid()) {
break; // Reached end of data
}
if (itemTask.canProcess()) {
// Stacking logic
changed = false;
for (EntityItem item : itemTask.getNearby()) {
for (Item item : itemTask.getNearby()) {
if (StackingTask.isMaxed(itemTask.getEntity())) {
break;
}
if (!item.dead && !StackingTask.isMaxed(item)) {
if (ItemUtil.transfer(item.itemStack, itemTask.getEntity().itemStack, Integer.MAX_VALUE) > 0) {
if (item.itemStack.count == 0) {
item.dead = true;
if (!item.isDead() && !StackingTask.isMaxed(item)) {
org.bukkit.inventory.ItemStack stack = item.getItemStack();
if (ItemUtil.transfer(stack, itemTask.getEntity().getItemStack(), Integer.MAX_VALUE) > 0) {
if (stack.getAmount() == 0) {
item.remove();
} else {
item.setItemStack(stack);
}
changed = true;
}
}
}
if (changed) {
ItemUtil.respawnItem(itemTask.getEntity());
ItemUtil.respawnItem(EntityUtil.getNative(itemTask.getEntity()));
}
}
}

// Finalize orb stacking
if (NoLaggItemStacker.stackOrbs) {
for (StackingTask<EntityExperienceOrb> orbTask : orbTasks) {
for (StackingTask<ExperienceOrb> orbTask : orbTasks) {
if (!orbTask.isValid()) {
break; // Reached end of data
}
if (orbTask.canProcess()) {
// Stacking logic
for (EntityExperienceOrb orb : orbTask.getNearby()) {
if (!orb.dead) {
orbTask.getEntity().value += orb.value;
orb.dead = true;
for (ExperienceOrb orb : orbTask.getNearby()) {
if (!orb.isDead()) {
ExperienceOrb e = orbTask.getEntity();
e.setExperience(e.getExperience() + orb.getExperience());
orb.remove();
}
}
}
Expand Down

0 comments on commit b352956

Please sign in to comment.