Search in sources :

Example 6 with Machine

use of com.easterlyn.machine.Machine in project Easterlyn by Easterlyn.

the class EasterlynMachines method loadMachine.

/**
 * Loads a machine from the given ConfigurationSection.
 *
 * @param key the machine's key location or null if it is to be parsed from storage
 * @param storage the ConfigurationSection used to store the machine data
 * @return a Pair containing the Machine and its corresponding data
 * @throws IllegalArgumentException if the provided ConfigurationSection cannot be used to load a
 *     Machine.
 */
@NotNull
private Pair<Machine, ConfigurationSection> loadMachine(@Nullable Block key, @NotNull ConfigurationSection storage) throws IllegalArgumentException {
    if (storage.getCurrentPath() == null) {
        throw new IllegalArgumentException("Machine storage must be a ConfigurationSection stored at its location.");
    }
    if (key == null) {
        key = locFromPath(storage.getCurrentPath()).getBlock();
    }
    if (getConfig().getStringList("+disabled-worlds+").contains(key.getWorld().getName())) {
        throw new IllegalArgumentException("Invalid machine at " + storage.getCurrentPath());
    }
    Machine machine = nameRegistry.get(storage.getString("type"));
    if (machine == null) {
        throw new IllegalArgumentException("Invalid machine at " + storage.getCurrentPath());
    }
    for (Block block : machine.getShape().getBuildLocations(key, machine.getDirection(storage)).keySet()) {
        blocksToKeys.put(block, key);
        keysToBlocks.put(key, block);
    }
    return new Pair<>(machine, storage);
}
Also used : Block(org.bukkit.block.Block) Machine(com.easterlyn.machine.Machine) Pair(com.github.jikoo.planarwrappers.tuple.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with Machine

use of com.easterlyn.machine.Machine in project Easterlyn by Easterlyn.

the class EasterlynMachines method registerInventoryEvent.

private <T extends InventoryEvent> void registerInventoryEvent(Class<T> clazz, Function<Machine, BiConsumer<T, ConfigurationSection>> consumer) {
    Event.register(clazz, event -> {
        Machine machine;
        ConfigurationSection section = null;
        Pair<Machine, ConfigurationSection> machineData = getInventoryMachine(event.getView().getTopInventory());
        if (machineData != null) {
            machine = machineData.getLeft();
            section = machineData.getRight();
        } else if (event.getView().getTopInventory().getHolder() instanceof Machine) {
            machine = (Machine) event.getView().getTopInventory().getHolder();
            if (event.getView().getTopInventory().getLocation() != null) {
                section = getConfig().getConfigurationSection(pathFromLoc(event.getView().getTopInventory().getLocation()));
            }
        } else {
            return;
        }
        if (machine == null) {
            return;
        }
        try {
            consumer.apply(machine).accept(event, section);
        } catch (Exception exception) {
            ReportableEvent.call("Caught exception handling Machine event", exception, 5);
        }
    }, this, EventPriority.LOW, true);
}
Also used : Machine(com.easterlyn.machine.Machine) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 8 with Machine

use of com.easterlyn.machine.Machine in project Easterlyn by Easterlyn.

the class MachineCommand method getMachine.

@CommandAlias("machine")
@Description("Machinations.")
@CommandPermission("easterlyn.command.machine")
@Syntax("<machine>")
@CommandCompletion("@machines")
public void getMachine(@Flags(CoreContexts.SELF) Player player, String machineName) {
    Machine machine = plugin.getByName(machineName);
    if (machine == null) {
        player.sendMessage("Invalid machine type!");
        return;
    }
    player.getWorld().dropItem(player.getLocation(), machine.getUniqueDrop()).setPickupDelay(0);
}
Also used : Machine(com.easterlyn.machine.Machine) Description(co.aikar.commands.annotation.Description) CommandCompletion(co.aikar.commands.annotation.CommandCompletion) Syntax(co.aikar.commands.annotation.Syntax) CommandPermission(co.aikar.commands.annotation.CommandPermission) CommandAlias(co.aikar.commands.annotation.CommandAlias)

Aggregations

Machine (com.easterlyn.machine.Machine)8 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)6 Block (org.bukkit.block.Block)4 Pair (com.github.jikoo.planarwrappers.tuple.Pair)3 Player (org.bukkit.entity.Player)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 CommandAlias (co.aikar.commands.annotation.CommandAlias)1 CommandCompletion (co.aikar.commands.annotation.CommandCompletion)1 CommandPermission (co.aikar.commands.annotation.CommandPermission)1 Description (co.aikar.commands.annotation.Description)1 Syntax (co.aikar.commands.annotation.Syntax)1 ReportableEvent (com.easterlyn.event.ReportableEvent)1 BlockUtil (com.easterlyn.util.BlockUtil)1 ItemUtil (com.easterlyn.util.inventory.ItemUtil)1 BlockMap (com.github.jikoo.planarwrappers.collections.BlockMap)1 BlockMultimap (com.github.jikoo.planarwrappers.collections.BlockMultimap)1 Event (com.github.jikoo.planarwrappers.event.Event)1 Coords (com.github.jikoo.planarwrappers.util.Coords)1 Direction (com.github.jikoo.planarwrappers.world.Direction)1