Search in sources :

Example 1 with IInventoryHandle

use of com.bergerkiller.generated.net.minecraft.world.IInventoryHandle in project BKCommonLib by bergerhealer.

the class CommonEntity method replaceEntity.

@SuppressWarnings("unchecked")
private void replaceEntity(final EntityHandle newInstance) {
    final EntityHandle oldInstance = this.handle;
    if (oldInstance.getRaw() == newInstance.getRaw()) {
        throw new RuntimeException("Can not replace an entity with itself!");
    }
    // *** Bukkit Entity ***
    CraftEntityHandle craftEntity = CraftEntityHandle.createHandle(this.entity);
    craftEntity.setHandle(newInstance);
    if (entity instanceof InventoryHolder) {
        Inventory inv = ((InventoryHolder) entity).getInventory();
        if (CraftInventoryHandle.T.isAssignableFrom(inv)) {
            CraftInventoryHandle cInv = CraftInventoryHandle.createHandle(inv);
            if (IInventoryHandle.T.isAssignableFrom(newInstance.getRaw())) {
                IInventoryHandle iinvHandle = IInventoryHandle.createHandle(newInstance.getRaw());
                cInv.setHandleField(iinvHandle);
            }
        }
    }
    // *** Give the old entity a new Bukkit Entity ***
    oldInstance.setBukkitEntityField(CraftEntityHandle.createCraftEntity(Bukkit.getServer(), oldInstance));
    this.handle = newInstance;
    // *** Replace entity in passenger and vehicle fields ***
    EntityHandle vehicle = newInstance.getVehicle();
    if (vehicle != null) {
        List<EntityHandle> passengers = new ArrayList<EntityHandle>(vehicle.getPassengers());
        replaceInList(passengers, newInstance);
        vehicle.setPassengers(passengers);
    }
    for (EntityHandle passenger : newInstance.getPassengers()) {
        if (oldInstance.getRaw() == passenger.getVehicle().getRaw()) {
            passenger.setVehicle(newInstance);
        }
    }
    // *** Replace data that is stored in the entity itself ***
    newInstance.assignEntityReference();
    // *** Perform further replacement all over the place in the server ***
    EntityAddRemoveHandler.INSTANCE.replace(oldInstance, newInstance);
    // *** Reset entity state ***
    oldInstance.setRemovedPassive();
    newInstance.setValid(true);
    // *** Repeat the replacement in the server the next tick to make sure nothing lingers ***
    CommonUtil.nextTick(() -> EntityAddRemoveHandler.INSTANCE.replace(oldInstance, newInstance));
    // *** Make sure a controller is set when hooked ***
    if (this.isHooked()) {
        DefaultEntityController controller = new DefaultEntityController();
        controller.bind(this, true);
    }
}
Also used : CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) IInventoryHandle(com.bergerkiller.generated.net.minecraft.world.IInventoryHandle) ArrayList(java.util.ArrayList) EntityHandle(com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle) CraftEntityHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle) InventoryHolder(org.bukkit.inventory.InventoryHolder) CraftInventoryHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftInventoryHandle) Inventory(org.bukkit.inventory.Inventory)

Example 2 with IInventoryHandle

use of com.bergerkiller.generated.net.minecraft.world.IInventoryHandle in project BKCommonLib by bergerhealer.

the class InventoryTest method testBaseInventory.

@Test
public void testBaseInventory() {
    InventoryBaseImpl inventory = new InventoryBaseImpl(9 * 3);
    assertEquals(9 * 3, inventory.getSize());
    for (int i = 0; i < inventory.getSize(); i++) {
        assertNull(inventory.getItem(i));
    }
    // Verify that the IInventory handle returns non-null items (uses EMPTY constant)
    IInventoryHandle handle = IInventoryHandle.createHandle(HandleConversion.toIInventoryHandle(inventory));
    assertEquals(9 * 3, handle.getSize());
    for (int i = 0; i < handle.getSize(); i++) {
        assertTrue(ItemStackHandle.EMPTY_ITEM.equals(handle.getItem(i)));
    }
    List<ItemStackHandle> handleContents = handle.getContents();
    assertEquals(9 * 3, handleContents.size());
    for (int i = 0; i < handleContents.size(); i++) {
        assertTrue(ItemStackHandle.EMPTY_ITEM.equals(handle.getItem(i)));
    }
    ItemStack testItem1 = new ItemStack(Material.GRASS, 1);
    inventory.setItem(2, testItem1);
    assertItemEquals(testItem1, inventory.getItem(2));
    ItemStack testItem2 = new ItemStack(Material.STONE, 16);
    int cnt = 0;
    for (int n = 0; n < 4; n++) {
        cnt += 16;
        inventory.addItem(testItem2);
        assertItemEquals(new ItemStack(Material.STONE, cnt), inventory.getItem(0));
    }
    inventory.addItem(testItem2);
    assertItemEquals(new ItemStack(Material.STONE, 64), inventory.getItem(0));
    assertItemEquals(new ItemStack(Material.STONE, 16), inventory.getItem(1));
}
Also used : InventoryBaseImpl(com.bergerkiller.bukkit.common.inventory.InventoryBaseImpl) IInventoryHandle(com.bergerkiller.generated.net.minecraft.world.IInventoryHandle) ItemStackHandle(com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle) ItemStack(org.bukkit.inventory.ItemStack) Test(org.junit.Test)

Aggregations

IInventoryHandle (com.bergerkiller.generated.net.minecraft.world.IInventoryHandle)2 InventoryBaseImpl (com.bergerkiller.bukkit.common.inventory.InventoryBaseImpl)1 EntityHandle (com.bergerkiller.generated.net.minecraft.world.entity.EntityHandle)1 ItemStackHandle (com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle)1 CraftEntityHandle (com.bergerkiller.generated.org.bukkit.craftbukkit.entity.CraftEntityHandle)1 CraftInventoryHandle (com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftInventoryHandle)1 ArrayList (java.util.ArrayList)1 Inventory (org.bukkit.inventory.Inventory)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 ItemStack (org.bukkit.inventory.ItemStack)1 Test (org.junit.Test)1