org.bukkit.entity.minecart.StorageMinecart Java Examples

The following examples show how to use org.bukkit.entity.minecart.StorageMinecart. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LWCPlayerListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler
public void minecartBreak(VehicleDestroyEvent e) {
    Entity entity = e.getVehicle();
    if (plugin.getLWC().isProtectable(e.getVehicle().getType())) {
        int A = 50000 + entity.getUniqueId().hashCode();
        LWC lwc = LWC.getInstance();
        Protection protection = lwc.getPhysicalDatabase().loadProtection(entity.getWorld().getName(), A, A, A);
        if ((((entity instanceof StorageMinecart)) || ((entity instanceof HopperMinecart)))
                && (protection != null)) {
            if (e.getAttacker() instanceof Projectile) {
                e.setCancelled(true);
            }
            Player p = (Player) e.getAttacker();
            boolean canAccess = lwc.canAccessProtection(p, protection);
            if (canAccess) {
                protection.remove();
                protection.removeAllPermissions();
                protection.removeCache();
                return;
            }
            e.setCancelled(true);
        }
    }
}
 
Example #2
Source File: LWCPlayerListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler
public void storageMinecraftInventoryOpen(InventoryOpenEvent event) {
    InventoryHolder holder = event.getInventory().getHolder();
    Player player = (Player) event.getPlayer();
    if ((!(holder instanceof StorageMinecart)) && (!(holder instanceof HopperMinecart))) {
        return;
    }
    Entity entity = (Entity) holder;
    if (plugin.getLWC().isProtectable(entity.getType())) {
        if (!plugin.getLWC().hasPermission(player, "lwc.protect")
                && plugin.getLWC().hasPermission(player, "lwc.deny") && !plugin.getLWC().isAdmin(player)
                && !plugin.getLWC().isMod(player)) {
            plugin.getLWC().sendLocale(player, "protection.interact.error.blocked");
            event.setCancelled(true);
            return;
        }
        if (onPlayerEntityInteract((Player) event.getPlayer(), entity, event.isCancelled())) {
            event.setCancelled(true);
        }
    }
}
 
Example #3
Source File: LWCPlayerListener.java    From Modern-LWC with MIT License 5 votes vote down vote up
@EventHandler
public void itemFrameItemRemoval(EntityDamageByEntityEvent e) {
    if (e.isCancelled())
        return;
    Entity entity = e.getEntity();
    if (plugin.getLWC().isProtectable(e.getEntity().getType())) {
        int A = 50000 + entity.getUniqueId().hashCode();
        LWC lwc = LWC.getInstance();
        Protection protection = lwc.getPhysicalDatabase().loadProtection(entity.getWorld().getName(), A, A, A);
        if (!(entity instanceof Player)) {
            if (e.getDamager() instanceof Projectile) {
                if (protection != null) {
                    e.setCancelled(true);
                }
                if ((((entity instanceof StorageMinecart)) || ((entity instanceof HopperMinecart)))
                        && (protection != null)) {
                    e.setCancelled(true);
                }
            }
            if (e.getDamager() instanceof Player) {
                Player p = (Player) e.getDamager();
                if (protection != null && !lwc.canAccessProtection(p, protection)) {
                    e.setCancelled(true);
                }
                if (p.hasPermission("lwc.lockentity." + e.getEntityType())
                        || p.hasPermission("lwc.lockentity.all")) {
                    if (onPlayerEntityInteract(p, entity, e.isCancelled())) {
                        e.setCancelled(true);
                    }
                }
                if ((((entity instanceof StorageMinecart)) || ((entity instanceof HopperMinecart)))
                        && (protection != null)) {
                    e.setCancelled(true);
                }
            }
        }
    }
}
 
Example #4
Source File: ConveyorEffect.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onChunkLoad(ChunkLoadEvent event) {
    Chunk chunk = event.getChunk();
    for (Region r : new HashMap<>(orphanCarts).keySet()) {
        StorageMinecart sm = orphanCarts.get(r);
        if (Util.isWithinChunk(chunk, sm.getLocation())) {
            carts.put(r, sm);
            orphanCarts.remove(r);
        }
    }
}
 
Example #5
Source File: ConveyorEffect.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onChunkUnload(ChunkUnloadEvent event) {
    Chunk chunk = event.getChunk();
    for (Region r : new HashMap<>(carts).keySet()) {
        StorageMinecart sm = carts.get(r);
        if (Util.isWithinChunk(chunk, sm.getLocation())) {
            carts.remove(r);
            orphanCarts.put(r, sm);
        }
    }
}
 
Example #6
Source File: ConveyorEffect.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
private void returnCart(Region region, boolean removeFromCarts) {
    if (!carts.containsKey(region)) {
        return;
    }
    StorageMinecart sm = carts.get(region);
    try {
        CVInventory returnInventory = UnloadedInventoryHandler.getInstance().getChestInventory(region.getLocation());
        if (returnInventory.firstEmpty() > -1) {
            returnInventory.addItem(new ItemStack(Material.CHEST_MINECART));
        } else {
            returnInventory.setItem(returnInventory.getSize() - 1,
                    new ItemStack(Material.CHEST_MINECART));
        }

        for (ItemStack itemStack : sm.getInventory()) {
            if (returnInventory.firstEmpty() < 0) {
                break;
            }
            if (itemStack == null || itemStack.getType() == Material.AIR) {
                continue;
            }
            returnInventory.addItem(itemStack);
        }
        sm.getInventory().clear();
        sm.remove();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (removeFromCarts) {
        carts.remove(region);
    }
}
 
Example #7
Source File: ConveyorEffect.java    From Civs with GNU General Public License v3.0 5 votes vote down vote up
public void onDisable() {
    disabled = true;
    HashMap<Region, StorageMinecart> tempCarts = new HashMap<>(carts);
    for (Region region : tempCarts.keySet()) {
        returnCart(region, true);
    }
}
 
Example #8
Source File: StoreageMinecartBlock.java    From Modern-LWC with MIT License 4 votes vote down vote up
public StoreageMinecartBlock(StorageMinecart minecart) {
    super(minecart);
    this.minecart = minecart;
}
 
Example #9
Source File: StoreageMinecartBlock.java    From Modern-LWC with MIT License 4 votes vote down vote up
public StorageMinecart getMinecart() {
    return this.minecart;
}
 
Example #10
Source File: ConveyorEffect.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
private void handleExistingCarts(Region r) {
        if (!carts.containsKey(r)) {
            return;
        }

        StorageMinecart sm = carts.get(r);
        if (sm.isDead()) {
            carts.remove(r);
            return;
        }
        if (!sm.isValid() && !Util.isChunkLoadedAt(sm.getLocation())) {
            carts.remove(r);
            orphanCarts.put(r, sm);
            return;
        }

//        if (!Util.isLocationWithinSightOfPlayer(sm.getLocation())) {
//            returnCart(r, true);
//            return;
//        }

        Region region = RegionManager.getInstance().getRegionAt(sm.getLocation());
        if (region == null || region.equals(r)) {
            return;
        }

        CVInventory destinationInventory = UnloadedInventoryHandler.getInstance().getChestInventory(region.getLocation());
        HashSet<ItemStack> cartInventory = new HashSet<>(Arrays.asList(sm.getInventory().getContents()));

        CVInventory originInv = UnloadedInventoryHandler.getInstance().getChestInventory(carts.get(r).getLocation());
        boolean isDestinationChestFull = false;
        for (ItemStack is : cartInventory) {
            if (is == null || is.getType() == Material.AIR) {
                continue;
            }
            try {
                if (!isDestinationChestFull) {
                    if (destinationInventory.firstEmpty() < 0) {
                        isDestinationChestFull = true;
                        if (originInv == null || originInv.firstEmpty() < 0) {
                            break;
                        } else {
                            originInv.addItem(is);
                            sm.getInventory().removeItem(is);
                        }
                    }
                    if (ConfigManager.getInstance().isDebugLog()) {
                        DebugLogger.inventoryModifications++;
                    }
                    sm.getInventory().removeItem(is);
                    destinationInventory.addItem(is);
                    RegionManager.getInstance().removeCheckedRegion(region);
                } else {
                    sm.getInventory().removeItem(is);
                    if (ConfigManager.getInstance().isDebugLog()) {
                        DebugLogger.inventoryModifications++;
                    }
                    originInv.addItem(is);
                }
            } catch (NullPointerException npe) {
                npe.printStackTrace();
                break;
            }
        }
        returnCart(r, false);
        carts.remove(r);
        if (!cacheDestinationRegions.containsKey(r)) {
            cacheDestinationRegions.put(r, region);
        }
    }