org.bukkit.block.Dispenser Java Examples

The following examples show how to use org.bukkit.block.Dispenser. 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: MagicWorkbench.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispenser = locateDispenser(b);

    if (dispenser == null) {
        // How even...
        return;
    }

    Inventory inv = ((Dispenser) dispenser.getState()).getInventory();
    List<ItemStack[]> inputs = RecipeType.getRecipeInputList(this);

    for (int i = 0; i < inputs.size(); i++) {
        if (isCraftable(inv, inputs.get(i))) {
            ItemStack output = RecipeType.getRecipeOutputList(this, inputs.get(i)).clone();

            if (Slimefun.hasUnlocked(p, output, true)) {
                craft(inv, dispenser, p, b, output);
            }

            return;
        }
    }
    SlimefunPlugin.getLocalization().sendMessage(p, "machines.pattern-not-found", true);
}
 
Example #2
Source File: DispenserListener.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler
public void onBlockDispensing(BlockDispenseEvent e) {
    Block b = e.getBlock();

    if (b.getType() == Material.DISPENSER && b.getRelative(BlockFace.DOWN).getType() != Material.HOPPER) {
        SlimefunItem machine = BlockStorage.check(b);

        if (machine != null) {
            machine.callItemHandler(BlockDispenseHandler.class, handler -> {
                Dispenser dispenser = (Dispenser) b.getState();
                BlockFace face = ((Directional) b.getBlockData()).getFacing();
                Block block = b.getRelative(face);
                handler.onBlockDispense(e, dispenser, block, machine);
            });
        }
    }
}
 
Example #3
Source File: ProgrammableAndroid.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
protected void depositItems(BlockMenu menu, Block facedBlock) {
    if (facedBlock.getType() == Material.DISPENSER && BlockStorage.check(facedBlock, "ANDROID_INTERFACE_ITEMS")) {
        Dispenser d = (Dispenser) facedBlock.getState();

        for (int slot : getOutputSlots()) {
            ItemStack stack = menu.getItemInSlot(slot);

            if (stack != null) {
                Optional<ItemStack> optional = d.getInventory().addItem(stack).values().stream().findFirst();

                if (optional.isPresent()) {
                    menu.replaceExistingItem(slot, optional.get());
                }
                else {
                    menu.replaceExistingItem(slot, null);
                }
            }
        }
    }
}
 
Example #4
Source File: BlockPlacer.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
private void placeSlimefunBlock(SlimefunItem sfItem, ItemStack item, Block block, Dispenser dispenser) {
    block.setType(item.getType());
    BlockStorage.store(block, sfItem.getID());
    block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, item.getType());

    if (item.getType() == Material.SPAWNER && sfItem instanceof RepairedSpawner) {
        Optional<EntityType> entity = ((RepairedSpawner) sfItem).getEntityType(item);

        if (entity.isPresent()) {
            CreatureSpawner spawner = (CreatureSpawner) block.getState();
            spawner.setSpawnedType(entity.get());
            spawner.update(true, false);
        }
    }

    if (dispenser.getInventory().containsAtLeast(item, 2)) {
        dispenser.getInventory().removeItem(new CustomItem(item, 1));
    }
    else {
        Slimefun.runSync(() -> dispenser.getInventory().removeItem(item), 2L);
    }
}
 
Example #5
Source File: EnhancedCraftingTable.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispenser = b.getRelative(BlockFace.DOWN);
    Dispenser disp = (Dispenser) dispenser.getState();
    Inventory inv = disp.getInventory();

    List<ItemStack[]> inputs = RecipeType.getRecipeInputList(this);

    for (int i = 0; i < inputs.size(); i++) {
        if (isCraftable(inv, inputs.get(i))) {
            ItemStack output = RecipeType.getRecipeOutputList(this, inputs.get(i)).clone();
            if (Slimefun.hasUnlocked(p, output, true)) {
                craft(inv, dispenser, p, b, output);
            }

            return;
        }
    }
    SlimefunPlugin.getLocalization().sendMessage(p, "machines.pattern-not-found", true);
}
 
Example #6
Source File: TNTMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void dispenserNukes(BlockTransformEvent event) {
    BlockState oldState = event.getOldState();
    if(oldState instanceof Dispenser &&
       this.properties.dispenserNukeLimit > 0 &&
       this.properties.dispenserNukeMultiplier > 0 &&
       event.getCause() instanceof EntityExplodeEvent) {

        EntityExplodeEvent explodeEvent = (EntityExplodeEvent) event.getCause();
        Dispenser dispenser = (Dispenser) oldState;
        int tntLimit = Math.round(this.properties.dispenserNukeLimit / this.properties.dispenserNukeMultiplier);
        int tntCount = 0;

        for(ItemStack stack : dispenser.getInventory().contents()) {
            if(stack != null && stack.getType() == Material.TNT) {
                int transfer = Math.min(stack.getAmount(), tntLimit - tntCount);
                if(transfer > 0) {
                    stack.setAmount(stack.getAmount() - transfer);
                    tntCount += transfer;
                }
            }
        }

        tntCount = (int) Math.ceil(tntCount * this.properties.dispenserNukeMultiplier);

        for(int i = 0; i < tntCount; i++) {
            TNTPrimed tnt = this.getMatch().getWorld().spawn(BlockUtils.base(dispenser), TNTPrimed.class);

            tnt.setFuseTicks(10 + this.getMatch().getRandom().nextInt(10)); // between 0.5 and 1.0 seconds, same as vanilla TNT chaining

            Random random = this.getMatch().getRandom();
            Vector velocity = new Vector(random.nextGaussian(), random.nextGaussian(), random.nextGaussian()); // uniform random direction
            velocity.normalize().multiply(0.5 + 0.5 * random.nextDouble());
            tnt.setVelocity(velocity);

            callPrimeEvent(tnt, explodeEvent.getEntity(), false);
        }
    }
}
 
Example #7
Source File: Tnt.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onEntityExplode(EntityExplodeEvent event) {
    if (GameHandler.getGameHandler().getMatch().isRunning() && event.getEntity() instanceof TNTPrimed) {
        if (!blockDamage) {
            event.blockList().clear();
        } else if (yield != 0.3){
            event.setYield((float)yield);
        }
        UUID player = TntTracker.getWhoPlaced(event.getEntity());
        for (Block block : event.blockList()) {
            if (block.getState() instanceof Dispenser) {
                Inventory inventory = ((Dispenser) block.getState()).getInventory();
                Location location = block.getLocation();
                double tntCount = 0;
                for (ItemStack itemstack : inventory.getContents()) {
                    if (itemstack != null && itemstack.getType() == Material.TNT) tntCount += itemstack.getAmount() * multiplier;
                    if (tntCount >= limit) {
                        tntCount = limit;
                        break;
                    }
                }
                inventory.remove(Material.TNT);
                if (tntCount > 0) {
                    Random random = new Random();
                    for (double i = tntCount; i > 0; i--) {
                        TNTPrimed tnt = event.getWorld().spawn(location, TNTPrimed.class);
                        Vector velocity = new Vector((1.5 * random.nextDouble()) - 0.75, (1.5 * random.nextDouble()) - 0.75, (1.5 * random.nextDouble()) - 0.75);
                        tnt.setVelocity(velocity);
                        tnt.setFuseTicks(random.nextInt(10) + 10);
                        if (player != null) {
                            tnt.setMetadata("source", new FixedMetadataValue(Cardinal.getInstance(), player));
                        }
                    }
                }
            }
        }
    }
}
 
Example #8
Source File: AbstractSmeltery.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispBlock = b.getRelative(BlockFace.DOWN);
    Dispenser disp = (Dispenser) dispBlock.getState();
    Inventory inv = disp.getInventory();
    List<ItemStack[]> inputs = RecipeType.getRecipeInputList(this);

    for (int i = 0; i < inputs.size(); i++) {
        if (canCraft(inv, inputs, i)) {
            ItemStack output = RecipeType.getRecipeOutputList(this, inputs.get(i)).clone();

            if (Slimefun.hasUnlocked(p, output, true)) {
                Inventory outputInv = findOutputInventory(output, dispBlock, inv);

                if (outputInv != null) {
                    craft(p, b, inv, inputs.get(i), output, outputInv);
                }
                else {
                    SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
                }
            }

            return;
        }
    }

    SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example #9
Source File: Compressor.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
	Block dispBlock = b.getRelative(BlockFace.DOWN);
	Dispenser disp = (Dispenser) dispBlock.getState();
	Inventory inv = disp.getInventory();
	
	for (ItemStack item : inv.getContents()) {
		for (ItemStack recipeInput : RecipeType.getRecipeInputs(this)) {
			if (recipeInput != null && SlimefunUtils.isItemSimilar(item, recipeInput, true)) {
				ItemStack output = RecipeType.getRecipeOutput(this, recipeInput);
				Inventory outputInv = findOutputInventory(output, dispBlock, inv);
				
				if (outputInv != null) {
				    ItemStack removing = item.clone();
			        removing.setAmount(recipeInput.getAmount());
			        inv.removeItem(removing);

					craft(p, output, outputInv);
				}
				else {
				    SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
				}
				
				return;
			}
		}
	}
	
	SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example #10
Source File: ArmorForge.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispBlock = b.getRelative(BlockFace.DOWN);
    Dispenser disp = (Dispenser) dispBlock.getState();
    Inventory inv = disp.getInventory();
    List<ItemStack[]> inputs = RecipeType.getRecipeInputList(this);

    for (int i = 0; i < inputs.size(); i++) {
        if (isCraftable(inv, inputs.get(i))) {
            ItemStack output = RecipeType.getRecipeOutputList(this, inputs.get(i)).clone();

            if (Slimefun.hasUnlocked(p, output, true)) {
                Inventory outputInv = findOutputInventory(output, dispBlock, inv);

                if (outputInv != null) {
                    craft(p, output, inv, outputInv);
                }
                else {
                    SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
                }
            }
            
            return;
        }
    }

    SlimefunPlugin.getLocalization().sendMessage(p, "machines.pattern-not-found", true);
}
 
Example #11
Source File: OreCrusher.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispBlock = b.getRelative(BlockFace.DOWN);
    Dispenser disp = (Dispenser) dispBlock.getState();
    Inventory inv = disp.getInventory();

    for (ItemStack current : inv.getContents()) {
        for (ItemStack convert : RecipeType.getRecipeInputs(this)) {
            if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
                ItemStack adding = RecipeType.getRecipeOutput(this, convert);
                Inventory outputInv = findOutputInventory(adding, dispBlock, inv);
                if (outputInv != null) {
                    ItemStack removing = current.clone();
                    removing.setAmount(convert.getAmount());
                    inv.removeItem(removing);
                    outputInv.addItem(adding);
                    p.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, 1);
                }
                else SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);

                return;
            }
        }
    }

    SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example #12
Source File: Juicer.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
	Block dispBlock = b.getRelative(BlockFace.DOWN);
	Dispenser disp = (Dispenser) dispBlock.getState();
	Inventory inv = disp.getInventory();
	
	for (ItemStack current : inv.getContents()) {
		for (ItemStack convert : RecipeType.getRecipeInputs(this)) {
			if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
				ItemStack adding = RecipeType.getRecipeOutput(this, convert);
				Inventory outputInv = findOutputInventory(adding, dispBlock, inv);
				
				if (outputInv != null) {
					ItemStack removing = current.clone();
					removing.setAmount(1);
					inv.removeItem(removing);
					outputInv.addItem(adding);
					p.getWorld().playSound(b.getLocation(), Sound.ENTITY_PLAYER_SPLASH, 1F, 1F);
					p.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, Material.HAY_BLOCK);
				}
				else SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
				
				return;
			}
		}
	}
	
	SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example #13
Source File: PressureChamber.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispBlock = b.getRelative(BlockFace.UP).getRelative(BlockFace.UP);
    Dispenser disp = (Dispenser) dispBlock.getState();
    Inventory inv = disp.getInventory();

    for (ItemStack current : inv.getContents()) {
        for (ItemStack convert : RecipeType.getRecipeInputs(this)) {
            if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
                ItemStack output = RecipeType.getRecipeOutput(this, convert);
                Inventory outputInv = findOutputInventory(output, dispBlock, inv);

                if (outputInv != null) {
                    ItemStack removing = current.clone();
                    removing.setAmount(convert.getAmount());
                    inv.removeItem(removing);

                    craft(p, b, output, outputInv);
                }
                else SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);

                return;
            }
        }
    }
    SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example #14
Source File: GrindStone.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
	Block dispBlock = b.getRelative(BlockFace.DOWN);
	Dispenser disp = (Dispenser) dispBlock.getState();
	Inventory inv = disp.getInventory();
	
	for (ItemStack current : inv.getContents()) {
		for (ItemStack convert : RecipeType.getRecipeInputs(this)) {
			if (convert != null && SlimefunUtils.isItemSimilar(current, convert, true)) {
				ItemStack output = RecipeType.getRecipeOutput(this, convert);
				Inventory outputInv = findOutputInventory(output, dispBlock, inv);
				
				if (outputInv != null) {
					ItemStack removing = current.clone();
					removing.setAmount(1);
					inv.removeItem(removing);
					outputInv.addItem(output);
					p.getWorld().playSound(p.getLocation(), Sound.BLOCK_WOODEN_BUTTON_CLICK_ON, 1, 1);
				}
				else {
					SlimefunPlugin.getLocalization().sendMessage(p, "machines.full-inventory", true);
				}
				
				return;
			}
		}
	}
	SlimefunPlugin.getLocalization().sendMessage(p, "machines.unknown-material", true);
}
 
Example #15
Source File: BlockPlacer.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
private void placeBlock(ItemStack item, Block facedBlock, Dispenser dispenser) {
    facedBlock.setType(item.getType());

    if (item.hasItemMeta()) {
        ItemMeta meta = item.getItemMeta();

        if (meta.hasDisplayName()) {
            BlockState blockState = facedBlock.getState();

            if ((blockState instanceof Nameable)) {
                ((Nameable) blockState).setCustomName(meta.getDisplayName());
            }

            // Update block state after changing name
            blockState.update();
        }

    }

    facedBlock.getWorld().playEffect(facedBlock.getLocation(), Effect.STEP_SOUND, item.getType());

    if (dispenser.getInventory().containsAtLeast(item, 2)) {
        dispenser.getInventory().removeItem(new CustomItem(item, 1));
    }
    else {
        Slimefun.runSync(() -> dispenser.getInventory().removeItem(item), 2L);
    }
}
 
Example #16
Source File: ProgrammableAndroid.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
protected void refuel(BlockMenu menu, Block facedBlock) {
    if (facedBlock.getType() == Material.DISPENSER && BlockStorage.check(facedBlock, "ANDROID_INTERFACE_FUEL")) {
        Dispenser d = (Dispenser) facedBlock.getState();

        for (int slot = 0; slot < 9; slot++) {
            ItemStack item = d.getInventory().getItem(slot);

            if (item != null) {
                insertFuel(menu, d.getInventory(), slot, menu.getItemInSlot(43), item);
            }
        }
    }
}
 
Example #17
Source File: WarRegen.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public static void destroyThisBlock(Block blk, Town town) {
	
	WarRegen.saveBlock(blk, town.getName(), false);
			
	switch (blk.getType()) {
	case TRAPPED_CHEST:
		((Chest)blk.getState()).getBlockInventory().clear();
		break;
	case CHEST:
		((Chest)blk.getState()).getBlockInventory().clear();
		break;
	case DISPENSER:
		((Dispenser)blk.getState()).getInventory().clear();
		break;
	case BURNING_FURNACE:
	case FURNACE:
		((Furnace)blk.getState()).getInventory().clear();
		break;
	case DROPPER:
		((Dropper)blk.getState()).getInventory().clear();
		break;
	case HOPPER:
		((Hopper)blk.getState()).getInventory().clear();
		break;
	default:
		break;
	}
	
	ItemManager.setTypeId(blk, CivData.AIR);
	ItemManager.setData(blk, 0x0, true);
	
}
 
Example #18
Source File: WarRegen.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
private static void restoreBlockFromString(String line) {
	String[] split = line.split(":");
	
	int type = Integer.valueOf(split[0]);
	byte data = Byte.valueOf(split[1]);
	int x = Integer.valueOf(split[2]);
	int y = Integer.valueOf(split[3]);
	int z = Integer.valueOf(split[4]);
	String world = split[5];
	
	Block block = BukkitObjects.getWorld(world).getBlockAt(x,y,z);
	
	ItemManager.setTypeId(block, type);
	ItemManager.setData(block, data, false);

	// End of basic block info, try to get more now.
	Inventory inv = null;
	switch (block.getType()) {
	case TRAPPED_CHEST:
		inv = ((Chest)block.getState()).getBlockInventory();
		InventorySerializer.StringToInventory(inv, split[6]);
		break;
	case CHEST:
		inv = ((Chest)block.getState()).getBlockInventory();
		InventorySerializer.StringToInventory(inv, split[6]);
		break;
	case DISPENSER:			
		inv = ((Dispenser)block.getState()).getInventory();
		InventorySerializer.StringToInventory(inv, split[6]);
		break;
	case BURNING_FURNACE:
	case FURNACE:
		inv = ((Furnace)block.getState()).getInventory();
		InventorySerializer.StringToInventory(inv, split[6]);
		break;
	case DROPPER:
		inv = ((Dropper)block.getState()).getInventory();
		InventorySerializer.StringToInventory(inv, split[6]);
		break;
	case HOPPER:
		inv = ((Hopper)block.getState()).getInventory();
		InventorySerializer.StringToInventory(inv, split[6]);
		break;
	case SIGN:
	case SIGN_POST:
	case WALL_SIGN:
		Sign sign = (Sign)block.getState();
		String[] messages = split[6].split(",");
		for (int i = 0; i < 4; i++) {
			if (messages[i] != null) {
				sign.setLine(i, messages[i]);
			}
		}
		sign.update();
		break;
	default:
		break;
	}
	
	
}
 
Example #19
Source File: WarRegen.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
private static String blockToString(Block blk, boolean save_as_air) {
	if (save_as_air) { 
		return blockAsAir(blk);
	}
	else {
		String str = blockBasicString(blk);
		
		Inventory inv = null;
		switch (blk.getType()) {
		case TRAPPED_CHEST:
		case CHEST:
			inv = ((Chest)blk.getState()).getBlockInventory();
			str += blockInventoryString(inv);
			break;
		case DISPENSER:
			inv = ((Dispenser)blk.getState()).getInventory();
			str += blockInventoryString(inv);
			break;
		case BURNING_FURNACE:
		case FURNACE:
			inv = ((Furnace)blk.getState()).getInventory();
			str += blockInventoryString(inv);
			break;
		case DROPPER:
			inv = ((Dropper)blk.getState()).getInventory();
			str += blockInventoryString(inv);
			break;
		case HOPPER:
			inv = ((Hopper)blk.getState()).getInventory();
			str += blockInventoryString(inv);
			break;
		case SIGN:
		case SIGN_POST:
		case WALL_SIGN:
			Sign sign = (Sign)blk.getState();
			str += blockSignString(sign);
			break;
		default:
			break;
		}
		
		return str;
	}
}
 
Example #20
Source File: Kitchen.java    From ExoticGarden with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onInteract(Player p, Block b) {
    Block dispenser = b.getRelative(BlockFace.DOWN);

    Furnace furnace = locateFurnace(dispenser);
    FurnaceInventory furnaceInventory = furnace.getInventory();

    Inventory inv = ((Dispenser) dispenser.getState()).getInventory();
    List<ItemStack[]> inputs = RecipeType.getRecipeInputList(this);

    recipe:
    for (ItemStack[] input : inputs) {
        for (int i = 0; i < inv.getContents().length; i++) {
            if (!SlimefunUtils.isItemSimilar(inv.getContents()[i], input[i], true)) continue recipe;
        }

        ItemStack adding = RecipeType.getRecipeOutputList(this, input);

        if (Slimefun.hasUnlocked(p, adding, true)) {
            boolean canFit = furnaceInventory.getResult() == null || (furnaceInventory.getResult().getAmount() + adding.getAmount() <= 64 && SlimefunUtils.isItemSimilar(furnaceInventory.getResult(), adding, true));

            if (!canFit) {
                SlimefunPlugin.getLocal().sendMessage(p, "machines.full-inventory", true);
                return;
            }

            for (int i = 0; i < inv.getContents().length; i++) {
                ItemStack item = inv.getItem(i);

                if (item != null) {
                    ItemUtils.consumeItem(item, item.getType() == Material.MILK_BUCKET);
                }
            }

            Bukkit.getScheduler().runTaskLater(plugin, () -> p.getWorld().playSound(furnace.getLocation(), Sound.BLOCK_LAVA_EXTINGUISH, 1F, 1F), 55L);

            for (int i = 1; i < 7; i++) {
                Bukkit.getScheduler().runTaskLater(plugin, () -> p.getWorld().playSound(furnace.getLocation(), Sound.BLOCK_METAL_PLACE, 7F, 1F), i * 5L);
            }

            if (furnaceInventory.getResult() == null) {
                furnaceInventory.setResult(adding);
            }
            else {
                furnaceInventory.getResult().setAmount(furnaceInventory.getResult().getAmount() + adding.getAmount());
            }
        }

        return;
    }

    SlimefunPlugin.getLocal().sendMessage(p, "machines.pattern-not-found", true);
}
 
Example #21
Source File: TNTMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void dispenserNukes(BlockTransformEvent event) {
  BlockState oldState = event.getOldState();
  if (oldState instanceof Dispenser
      && this.properties.dispenserNukeLimit > 0
      && this.properties.dispenserNukeMultiplier > 0
      && event.getCause() instanceof EntityExplodeEvent) {

    EntityExplodeEvent explodeEvent = (EntityExplodeEvent) event.getCause();
    Dispenser dispenser = (Dispenser) oldState;
    int tntLimit =
        Math.round(this.properties.dispenserNukeLimit / this.properties.dispenserNukeMultiplier);
    int tntCount = 0;

    ItemStack[] inv = dispenser.getInventory().getContents();
    for (int slot = 0; slot < inv.length; slot++) {
      ItemStack stack = inv[slot];
      if (stack != null && stack.getType() == Material.TNT) {
        int transfer = Math.min(stack.getAmount(), tntLimit - tntCount);
        if (transfer > 0) {
          stack.setAmount(stack.getAmount() - transfer);
          tntCount += transfer;
          dispenser.getInventory().setItem(slot, stack);
        }
      }
    }

    tntCount = (int) Math.ceil(tntCount * this.properties.dispenserNukeMultiplier);

    for (int i = 0; i < tntCount; i++) {
      TNTPrimed tnt = match.getWorld().spawn(dispenser.getLocation(), TNTPrimed.class);

      tnt.setFuseTicks(
          10
              + match
                  .getRandom()
                  .nextInt(10)); // between 0.5 and 1.0 seconds, same as vanilla TNT chaining

      Random random = match.getRandom();
      Vector velocity =
          new Vector(
              random.nextGaussian(),
              random.nextGaussian(),
              random.nextGaussian()); // uniform random direction
      velocity.normalize().multiply(0.5 + 0.5 * random.nextDouble());
      tnt.setVelocity(velocity);

      callPrimeEvent(tnt, explodeEvent.getEntity());
    }
  }
}
 
Example #22
Source File: ObserverModule.java    From CardinalPGM with MIT License 4 votes vote down vote up
@EventHandler
public void onInteraction(PlayerInteractEvent event) {
    if (testObserver(event.getPlayer())) {
        event.setCancelled(true);
        if ((event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && (event.getPlayer().getInventory().getItemInMainHand() != null && event.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.WRITTEN_BOOK))){
            event.setUseInteractedBlock(Event.Result.DENY);
            event.setUseItemInHand(Event.Result.ALLOW);
        }
        if (event.getClickedBlock() != null && !event.getPlayer().isSneaking() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            if (event.getClickedBlock().getType().equals(Material.CHEST) || event.getClickedBlock().getType().equals(Material.TRAPPED_CHEST)) {
                Inventory chest = Bukkit.createInventory(null, ((Chest) event.getClickedBlock().getState()).getInventory().getSize());
                for (int i = 0; i < ((Chest) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    chest.setItem(i, ((Chest) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(chest);
            }
            if (event.getClickedBlock().getType().equals(Material.FURNACE) || event.getClickedBlock().getType().equals(Material.BURNING_FURNACE)) {
                Inventory furnace = Bukkit.createInventory(null, InventoryType.FURNACE);
                for (int i = 0; i < ((Furnace) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    furnace.setItem(i, ((Furnace) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(furnace);
            }
            if (event.getClickedBlock().getType().equals(Material.DISPENSER)) {
                Inventory dispenser = Bukkit.createInventory(null, InventoryType.DISPENSER);
                for (int i = 0; i < ((Dispenser) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    dispenser.setItem(i, ((Dispenser) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(dispenser);
            }
            if (event.getClickedBlock().getType().equals(Material.DROPPER)) {
                Inventory dropper = Bukkit.createInventory(null, InventoryType.DROPPER);
                for (int i = 0; i < ((Dropper) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    dropper.setItem(i, ((Dropper) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(dropper);
            }
            if (event.getClickedBlock().getType().equals(Material.HOPPER)) {
                Inventory hopper = Bukkit.createInventory(null, InventoryType.HOPPER);
                for (int i = 0; i < ((Hopper) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    hopper.setItem(i, ((Hopper) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(hopper);
            }
            if (event.getClickedBlock().getType().equals(Material.BREWING_STAND)) {
                Inventory brewingStand = Bukkit.createInventory(null, InventoryType.BREWING);
                for (int i = 0; i < ((BrewingStand) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    brewingStand.setItem(i, ((BrewingStand) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(brewingStand);
            }
            if (event.getClickedBlock().getType().equals(Material.BEACON)) {
                Inventory beacon = Bukkit.createInventory(null, InventoryType.BEACON);
                for (int i = 0; i < ((Beacon) event.getClickedBlock().getState()).getInventory().getSize(); i++) {
                    beacon.setItem(i, ((Beacon) event.getClickedBlock().getState()).getInventory().getItem(i));
                }
                event.getPlayer().openInventory(beacon);
            }
        }
    }
}
 
Example #23
Source File: BlockDispenseHandler.java    From Slimefun4 with GNU General Public License v3.0 votes vote down vote up
void onBlockDispense(BlockDispenseEvent e, Dispenser dispenser, Block facedBlock, SlimefunItem machine);