use of com.github.jikoo.planarwrappers.event.Event in project Easterlyn by Easterlyn.
the class MinecraftBridge method setup.
public void setup() {
client.getEventDispatcher().on(MessageCreateEvent.class).flatMap(event -> Mono.fromRunnable(() -> {
if (event.getMessage().getAuthor().isEmpty() || event.getMessage().getAuthor().get().isBot()) {
return;
}
User author = event.getMessage().getAuthor().get();
String msg = event.getMessage().getContent();
MessageChannel channel = event.getMessage().getChannel().block();
String command = null;
String commandData = null;
if (msg.startsWith(plugin.getCommandPrefix())) {
int prefixLength = plugin.getCommandPrefix().length();
int space = msg.indexOf(' ');
if (space < prefixLength) {
space = msg.length();
}
command = msg.substring(prefixLength, space).toLowerCase();
commandData = msg.substring(space < msg.length() ? space + 1 : space);
}
if (command != null && command.equals("link")) {
UUID pendingLink = plugin.getPendingLink(commandData);
if (pendingLink == null) {
channel.createMessage("Invalid link code! If your code expired, rerun `/link` in" + " Minecraft.\n" + "If you're having trouble, try clicking the message to" + " copy the code!");
} else {
plugin.addLink(pendingLink, author.getId());
}
}
boolean main = channel instanceof GuildMessageChannel && plugin.isChannelType(channel.getId(), ChannelType.MAIN);
if (!main && command == null) {
return;
}
DiscordUser sender = plugin.getUser(author.getId());
if (sender == null) {
Snowflake id = author.getId();
if (warnings.getIfPresent(id) != null) {
return;
}
warnings.put(id, true);
channel.createMessage(author.getMention() + ", you must run `/link` in Minecraft to use this" + " feature!").subscribe();
return;
}
if (command != null) {
handleCommand(sender, channel, command, commandData);
return;
}
handleDiscordChat(sender, event.getMessage());
}).onErrorResume(error -> {
System.out.println("Caught exception handling Discord event:");
error.printStackTrace();
return Mono.empty();
}).thenReturn(event)).subscribe();
Event.register(UserChatEvent.class, event -> {
// TODO may want to try to softdepend on chat instead of hard
if (event.isAsynchronous()) {
handleMinecraftChat(event);
} else {
// Ensure handling is done asynchronously even if chat is somehow sent on main thread
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> handleMinecraftChat(event));
}
}, plugin);
Event.register(PlayerCommandPreprocessEvent.class, event -> {
if (event.getPlayer().hasPermission("easterlyn.discord.exempt.commandlog")) {
return;
}
RegisteredServiceProvider<EasterlynCore> registration = plugin.getServer().getServicesManager().getRegistration(EasterlynCore.class);
if (registration == null) {
return;
}
int space = event.getMessage().indexOf(' ');
String commandName = event.getMessage().substring(1, space > 0 ? space : event.getMessage().length()).toLowerCase();
SimpleCommandMap commandMap = registration.getProvider().getSimpleCommandMap();
if (commandMap == null) {
return;
}
Command command = commandMap.getCommand(commandName);
if (command != null && !plugin.getConfig().getStringList("command-log-blacklist").contains(command.getName())) {
plugin.postMessage(ChannelType.LOG, event.getPlayer().getName() + " issued command: " + event.getMessage());
}
}, plugin, EventPriority.MONITOR);
Event.register(PlayerJoinEvent.class, event -> plugin.postMessage(ChannelType.MAIN, ChatColor.stripColor(event.getPlayer().getDisplayName()) + " logs in."), plugin, EventPriority.MONITOR);
Event.register(PlayerQuitEvent.class, event -> plugin.postMessage(ChannelType.MAIN, ChatColor.stripColor(event.getPlayer().getDisplayName()) + " logs out."), plugin);
}
use of com.github.jikoo.planarwrappers.event.Event in project Easterlyn by Easterlyn.
the class EasterlynEffects method applyEffects.
/**
* Applies all effects to the given LivingEntity.
*
* @param entity the LivingEntity
*/
private void applyEffects(@NotNull LivingEntity entity, @Nullable org.bukkit.event.Event event) {
EntityEquipment equipment = entity.getEquipment();
if (equipment == null) {
return;
}
Map<Effect, Integer> effects = new HashMap<>();
equipmentSlotMap.forEach((equipmentSlot, equipmentSlotFunction) -> getEffects(false, equipmentSlotFunction.apply(equipment)).forEach((effect, level) -> {
if (!effect.getTarget().apply(equipmentSlot)) {
return;
}
effects.compute(effect, (value, current) -> {
if (current == null) {
current = level;
} else {
current += level;
}
return current > effect.getMaxTotalLevel() ? effect.getMaxTotalLevel() : current;
});
}));
effects.forEach((effect, level) -> effect.applyEffect(entity, level, event));
}
use of com.github.jikoo.planarwrappers.event.Event 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);
}
Aggregations