use of com.easterlyn.machine.Machine in project Easterlyn by Easterlyn.
the class EasterlynMachines method loadChunkMachines.
private void loadChunkMachines(Chunk chunk) {
String worldName = chunk.getWorld().getName();
if (!getConfig().isSet(worldName) || getConfig().getStringList("+disabled-worlds+").contains(worldName)) {
return;
}
String path = chunk.getWorld().getName() + '.' + chunk.getX() + '_' + chunk.getZ();
if (!getConfig().isConfigurationSection(path)) {
return;
}
ConfigurationSection chunkSection = getConfig().getConfigurationSection(path);
if (chunkSection == null) {
return;
}
Set<String> chunkKeys = chunkSection.getKeys(false);
for (Iterator<String> iterator = chunkKeys.iterator(); iterator.hasNext(); ) {
String xyz = iterator.next();
ConfigurationSection machineSection = chunkSection.getConfigurationSection(xyz);
if (machineSection == null) {
iterator.remove();
continue;
}
String[] split = xyz.split("_");
try {
Block key = chunk.getWorld().getBlockAt(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
Pair<Machine, ConfigurationSection> machine = loadMachine(key, machineSection);
machine.getLeft().enable(machine.getRight());
} catch (NumberFormatException e) {
getLogger().warning("Machine cannot be parsed from " + Arrays.toString(split));
} catch (IllegalArgumentException e) {
iterator.remove();
}
}
if (chunkKeys.isEmpty()) {
getConfig().set(path, null);
}
}
use of com.easterlyn.machine.Machine in project Easterlyn by Easterlyn.
the class EasterlynMachines method onEnable.
@Override
public void onEnable() {
saveDefaultConfig();
new Reflections("com.easterlyn.machine", getClassLoader()).getSubTypesOf(Machine.class).stream().filter(clazz -> !Modifier.isAbstract(clazz.getModifiers())).forEach(clazz -> {
Constructor<? extends Machine> constructor;
Machine machine;
try {
constructor = clazz.getConstructor(this.getClass());
machine = constructor.newInstance(this);
} catch (ReflectiveOperationException e) {
getLogger().severe("Unable to load machine " + clazz.getName());
e.printStackTrace();
return;
}
nameRegistry.put(machine.getClass().getSimpleName().toLowerCase(), machine);
nameRegistry.put(machine.getName().toLowerCase(), machine);
iconRegistry.put(machine.getUniqueDrop(), machine);
});
Event.register(BlockPlaceEvent.class, event -> {
Player player = event.getPlayer();
Pair<Machine, ConfigurationSection> pair = getMachine(event.getBlock());
if (pair != null) {
// Block registered as part of a machine. Most likely removed by explosion or similar.
// Prevents place PGO as diamond block, blow up PGO, place and break dirt in PGO's
// location to unregister, wait for CreeperHeal to regenerate diamond block for profit.
event.setCancelled(true);
player.sendMessage("machines.noTouch");
// fire a reportable event and repair.
if (exploded.get(event.getBlock()) == null) {
pair.getLeft().reassemble(pair.getRight());
ReportableEvent.call(String.format("Repairing broken %s at %s %s %s after internal placement by %s", pair.getLeft().getName(), event.getBlock().getX(), event.getBlock().getY(), event.getBlock().getZ(), event.getPlayer().getName()));
}
return;
}
// Machine place logic
for (Map.Entry<ItemStack, Machine> entry : iconRegistry.entrySet()) {
if (entry.getKey().isSimilar(event.getItemInHand())) {
pair = createMachine(event.getBlock(), entry.getValue(), event.getPlayer().getUniqueId(), Direction.getFacingDirection(event.getPlayer()));
if (pair == null) {
event.setCancelled(true);
return;
}
if (pair.getLeft().failedAssembly(pair.getRight())) {
event.setCancelled(true);
}
if (!event.isCancelled() && player.getGameMode() != GameMode.CREATIVE) {
if (player.getInventory().getItemInMainHand().equals(event.getItemInHand())) {
player.getInventory().setItemInMainHand(ItemUtil.decrement(event.getItemInHand(), 1));
} else {
player.getInventory().setItemInOffHand(ItemUtil.decrement(event.getItemInHand(), 1));
}
}
event.setCancelled(true);
break;
}
}
}, this, EventPriority.HIGHEST);
hookCreeperHeal();
// Machine event handlers
Event.register(PlayerInteractEvent.class, event -> {
if (event.getClickedBlock() == null) {
return;
}
Pair<Machine, ConfigurationSection> machine = getMachine(event.getClickedBlock());
if (machine != null) {
machine.getLeft().handleInteract(event, machine.getRight());
}
}, this, EventPriority.LOW);
Event.register(InventoryMoveItemEvent.class, event -> {
InventoryHolder inventoryHolder = event.getDestination().getHolder();
// For now, sending inv is not checked, as no machines require it.
if (inventoryHolder instanceof BlockState) {
Pair<Machine, ConfigurationSection> pair = getMachine(((BlockState) inventoryHolder).getBlock());
if (pair != null) {
pair.getLeft().handleHopperMoveItem(event, pair.getRight());
}
}
}, this, EventPriority.LOW);
Event.register(InventoryPickupItemEvent.class, event -> {
InventoryHolder inventoryHolder = event.getInventory().getHolder();
// For now, sending inv is not checked, as no machines require it.
if (inventoryHolder instanceof BlockState) {
Pair<Machine, ConfigurationSection> pair = getMachine(((BlockState) inventoryHolder).getBlock());
if (pair != null) {
pair.getLeft().handleHopperPickupItem(event, pair.getRight());
}
}
}, this, EventPriority.LOW);
registerInventoryEvent(InventoryClickEvent.class, machine -> machine::handleClick);
registerInventoryEvent(InventoryDragEvent.class, machine -> machine::handleDrag);
registerInventoryEvent(InventoryOpenEvent.class, machine -> machine::handleOpen);
registerInventoryEvent(InventoryCloseEvent.class, machine -> machine::handleClose);
Event.register(InventoryCloseEvent.class, event -> {
if (event.getView().getTopInventory() instanceof MerchantInventory) {
merchants.remove(((MerchantInventory) event.getView().getTopInventory()).getMerchant());
}
}, this, EventPriority.HIGH);
Event.register(TradeSelectEvent.class, event -> {
Pair<Machine, ConfigurationSection> machineData = merchants.get(event.getMerchant());
if (machineData != null) {
machineData.getLeft().selectTrade(event, machineData.getRight());
}
}, this);
Event.register(BlockPistonExtendEvent.class, event -> {
if (isMachine(event.getBlock()) || event.getBlocks().stream().anyMatch(this::isMachine)) {
event.setCancelled(true);
}
}, this, EventPriority.LOW);
Event.register(BlockPistonRetractEvent.class, event -> {
if (isMachine(event.getBlock()) || event.getBlocks().stream().anyMatch(this::isMachine)) {
event.setCancelled(true);
}
}, this, EventPriority.LOW);
Event.register(PlayerBucketEmptyEvent.class, event -> {
if (isMachine(event.getBlockClicked().getRelative(event.getBlockFace()))) {
// If we do end up creating a lava well etc. this will need to be added to an event.
event.setCancelled(true);
}
}, this, EventPriority.LOW);
Consumer<PlayerBucketEvent> bucketConsumer = event -> {
if (isMachine(event.getBlockClicked().getRelative(event.getBlockFace()))) {
event.setCancelled(true);
}
};
Event.register(PlayerBucketEmptyEvent.class, bucketConsumer::accept, this, EventPriority.LOW);
Event.register(PlayerBucketFillEvent.class, bucketConsumer::accept, this, EventPriority.LOW);
registerBlockEvent(BlockBreakEvent.class, machine -> machine::handleBreak);
registerBlockEvent(BlockFadeEvent.class, machine -> machine::handleFade);
registerBlockEvent(BlockFromToEvent.class, machine -> machine::handleFromTo);
registerBlockEvent(BlockIgniteEvent.class, machine -> machine::handleIgnite);
registerBlockEvent(BlockSpreadEvent.class, machine -> machine::handleSpread);
// Prevent all external machine manipulation
Event.register(BlockPhysicsEvent.class, event -> {
if (isMachine(event.getBlock())) {
event.setCancelled(true);
}
}, this);
Event.register(EntityChangeBlockEvent.class, event -> {
if (isMachine(event.getBlock())) {
event.setCancelled(true);
}
}, this);
Event.register(ChunkLoadEvent.class, event -> getServer().getScheduler().runTask(this, () -> loadChunkMachines(event.getChunk())), this);
// TODO periodic save system (timer triggered by chunk unload?)
Event.register(ChunkUnloadEvent.class, event -> getServer().getScheduler().runTask(this, () -> unloadChunkMachines(event.getChunk())), this);
for (World world : getServer().getWorlds()) {
if (getConfig().getStringList("+disabled-worlds+").contains(world.getName())) {
continue;
}
for (Chunk chunk : world.getLoadedChunks()) {
loadChunkMachines(chunk);
}
}
RegisteredServiceProvider<EasterlynCore> registration = getServer().getServicesManager().getRegistration(EasterlynCore.class);
if (registration != null) {
register(registration.getProvider());
}
Event.register(PluginEnableEvent.class, pluginEnableEvent -> {
if (pluginEnableEvent.getPlugin() instanceof EasterlynCore) {
register((EasterlynCore) pluginEnableEvent.getPlugin());
}
}, this);
}
use of com.easterlyn.machine.Machine in project Easterlyn by Easterlyn.
the class EasterlynMachines method unloadChunkMachines.
private void unloadChunkMachines(Chunk chunk) {
blocksToKeys.remove(chunk).stream().distinct().forEach(key -> {
ConfigurationSection section = getConfig().getConfigurationSection(pathFromLoc(key.getLocation()));
if (section == null) {
return;
}
String type = section.getString("type");
Machine machine = nameRegistry.get(type);
if (machine == null) {
return;
}
machine.disable(section);
});
keysToBlocks.remove(chunk);
}
use of com.easterlyn.machine.Machine in project Easterlyn by Easterlyn.
the class EasterlynMachines method onDisable.
@Override
public void onDisable() {
for (Player player : getServer().getOnlinePlayers()) {
if (getInventoryMachine(player.getOpenInventory().getTopInventory()) != null) {
player.closeInventory();
}
}
keysToBlocks.entrySet().stream().distinct().forEach(entry -> {
ConfigurationSection section = getConfig().getConfigurationSection(pathFromLoc(entry.getKey().getLocation()));
if (section == null) {
return;
}
String type = section.getString("type");
Machine machine = nameRegistry.get(type);
if (machine == null) {
return;
}
machine.disable(section);
});
saveConfig();
iconRegistry.clear();
nameRegistry.clear();
}
use of com.easterlyn.machine.Machine in project Easterlyn by Easterlyn.
the class EasterlynMachines method getMachine.
/**
* Gets a Machine by Block.
*
* @param block the Block
* @return the Machine or null if the Block is not part of a Machine
* @throws IllegalStateException if the Machine is malformed
*/
@Nullable
private Pair<Machine, ConfigurationSection> getMachine(@NotNull Block block) throws IllegalStateException {
Block key = blocksToKeys.get(block);
if (key == null) {
return null;
}
ConfigurationSection section = getConfig().getConfigurationSection(pathFromLoc(key.getLocation()));
if (section == null) {
throw new IllegalStateException("No ConfigurationSection available for stored key block!");
}
String type = section.getString("type");
Machine machine = nameRegistry.get(type);
if (machine == null) {
throw new IllegalStateException(String.format("Invalid machine type specified in ConfigurationSection: %s", type));
}
return new Pair<>(machine, section);
}
Aggregations