Single module setup

This commit is contained in:
Draycia committed 2020-08-21 22:04:21 -07:00
1 parent ad9a794156
commit 66217f6534
70 files changed
+92 -138

No files matched your search

@@ -0,0 +1,11 @@
package net.draycia.carbon.channels.contexts;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.storage.ChatUser;
public interface MessageContext {
ChatUser getSender();
ChatUser getTarget();
ChatChannel getChatChannel();
Object getValue();
}
@@ -0,0 +1,32 @@
package net.draycia.carbon.channels.contexts.impl;
import net.draycia.carbon.channels.contexts.MessageContext;
import org.bukkit.Location;
import java.util.function.Function;
public class DistanceContext implements Function<MessageContext, Boolean> {
@Override
public Boolean apply(MessageContext context) {
if (!context.getSender().isOnline() || !context.getTarget().isOnline()) {
return false;
}
Location senderLocation = context.getSender().asPlayer().getLocation();
Location targetLocation = context.getTarget().asPlayer().getLocation();
if (!senderLocation.getWorld().equals(targetLocation.getWorld())) {
return false;
}
Double value = (Double) context.getValue();
if (value > 0) {
return senderLocation.distance(targetLocation) <= value;
}
return true;
}
}
@@ -0,0 +1,39 @@
package net.draycia.carbon.channels.contexts.impl;
import net.draycia.carbon.channels.ChatChannel;
import net.draycia.carbon.channels.contexts.MessageContext;
import net.draycia.carbon.storage.ChatUser;
public class SimpleMessageContext implements MessageContext {
private final ChatUser sender;
private final ChatUser target;
private final ChatChannel chatChannel;
private final Object value;
public SimpleMessageContext(ChatUser sender, ChatUser target, ChatChannel chatChannel, Object value) {
this.sender = sender;
this.target = target;
this.chatChannel = chatChannel;
this.value = value;
}
@Override
public ChatUser getSender() {
return sender;
}
@Override
public ChatUser getTarget() {
return target;
}
@Override
public ChatChannel getChatChannel() {
return chatChannel;
}
@Override
public Object getValue() {
return value;
}
}
@@ -0,0 +1,105 @@
package net.draycia.carbon.channels.contexts.impl;
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Resident;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.events.ChannelSwitchEvent;
import net.draycia.carbon.events.PreChatFormatEvent;
import net.draycia.carbon.storage.ChatUser;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public final class TownyContext implements Listener {
private final CarbonChat carbonChat;
private static final String KEY = "towny-town";
public TownyContext(CarbonChat carbonChat) {
this.carbonChat = carbonChat;
this.carbonChat.getContextManager().register("towny-town", (context) -> {
if ((context.getValue() instanceof Boolean) && ((Boolean) context.getValue())) {
return isInSameTown(context.getSender(), context.getTarget());
}
return true;
});
}
@EventHandler(ignoreCancelled = true)
public void onChannelSwitch(ChannelSwitchEvent event) {
Object town = event.getChannel().getContext(KEY);
if ((town instanceof Boolean) && ((Boolean) town)) {
if (!isInTown(event.getUser())) {
event.setCancelled(true);
event.setFailureMessage(carbonChat.getConfig().getString("contexts.Towny.cancellation-message"));
}
}
}
@EventHandler(ignoreCancelled = true)
public void onChannelMessage(PreChatFormatEvent event) {
// TODO: event.setFailureMessage
Object town = event.getChannel().getContext(KEY);
if ((town instanceof Boolean) && ((Boolean) town)) {
if (!isInTown(event.getUser())) {
event.setCancelled(true);
}
}
}
@EventHandler
public void onResidentRemove(TownRemoveResidentEvent event) {
String name = event.getResident().getName();
ChatUser user = carbonChat.getUserService().wrap(name);
Object town = user.getSelectedChannel().getContext(KEY);
if ((town instanceof Boolean) && ((Boolean) town)) {
user.clearSelectedChannel();
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
Object town = user.getSelectedChannel().getContext(KEY);
if ((town instanceof Boolean) && ((Boolean) town) && !isInTown(user)) {
user.clearSelectedChannel();
}
}
public boolean isInTown(ChatUser user) {
try {
return TownyAPI.getInstance().getDataSource().getResident(user.asPlayer().getName()).hasTown();
} catch (NotRegisteredException e) {
e.printStackTrace();
}
return false;
}
public boolean isInSameTown(ChatUser user1, ChatUser user2) {
if (!user1.isOnline() || !user2.isOnline()) {
return false;
}
try {
Resident resident = TownyAPI.getInstance().getDataSource().getResident(user1.asPlayer().getName());
if (resident.hasTown()) {
return resident.getTown().hasResident(user2.asPlayer().getName());
}
} catch (NotRegisteredException e) {
e.printStackTrace();
}
return false;
}
}
@@ -0,0 +1,129 @@
package net.draycia.carbon.channels.contexts.impl;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.events.ChannelSwitchEvent;
import net.draycia.carbon.events.PreChatFormatEvent;
import net.draycia.carbon.storage.ChatUser;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.util.List;
public final class WorldGuardContext implements Listener {
private static final String KEY = "worldguard-region";
public WorldGuardContext(CarbonChat carbonChat) {
carbonChat.getContextManager().register(KEY, (context) -> {
return this.testContext(context.getSender(), context.getTarget(), context.getValue());
});
}
@EventHandler(ignoreCancelled = true)
public void onChannelSwitch(ChannelSwitchEvent event) {
// TODO: cancellation message
Object value = event.getChannel().getContext(KEY);
if ((value instanceof String || value instanceof List) && !isInRegionOrRegions(value, event.getUser())) {
event.setCancelled(true);
}
}
@EventHandler(ignoreCancelled = true)
public void onChannelMessage(PreChatFormatEvent event) {
// TODO: cancellation message
Object value = event.getChannel().getContext(KEY);
if ((value instanceof String || value instanceof List) && !isInRegionOrRegions(value, event.getUser())) {
event.setCancelled(true);
}
}
private boolean isInRegionOrRegions(Object value, ChatUser user) {
if ((value instanceof String)) {
return isInRegion((String) value, user);
}
if (value instanceof List) {
for (String region : ((List<String>) value)) {
if (isInRegion(region, user)) {
return true;
}
}
}
return false;
}
public boolean testContext(ChatUser sender, ChatUser target, Object value) {
boolean user1InRegion = false;
boolean user2InRegion = false;
if ((value instanceof Boolean) && ((Boolean) value)) {
return isInSameRegion(sender, target);
} else if (value instanceof String) {
user1InRegion = isInRegion((String)value, sender);
user2InRegion = isInRegion((String)value, target);
} else if (value instanceof List) {
for (String item : (List<String>)value) {
if (!user1InRegion) {
user1InRegion = isInRegion(item, sender);
}
if (!user2InRegion) {
user2InRegion = isInRegion(item, target);
}
}
} else {
return true;
}
return user1InRegion && user2InRegion;
}
public boolean isInSameRegion(ChatUser user1, ChatUser user2) {
if (!user1.isOnline() || !user2.isOnline()) {
return false;
}
Location user1Location = BukkitAdapter.adapt(user1.asPlayer().getLocation());
Location user2Location = BukkitAdapter.adapt(user2.asPlayer().getLocation());
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery regionQuery = container.createQuery();
ApplicableRegionSet user1Regions = regionQuery.getApplicableRegions(user1Location);
ApplicableRegionSet user2Regions = regionQuery.getApplicableRegions(user2Location);
for (ProtectedRegion region : user1Regions) {
if (user2Regions.getRegions().contains(region)) {
return true;
}
}
return false;
}
public boolean isInRegion(String region, ChatUser user) {
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
World world = BukkitAdapter.adapt(user.asPlayer().getWorld());
ProtectedRegion protection = container.get(world).getRegion(region);
return isInRegion(protection, user);
}
public boolean isInRegion(ProtectedRegion region, ChatUser user) {
Location location = BukkitAdapter.adapt(user.asPlayer().getLocation());
return region.contains(location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
}
@@ -0,0 +1,91 @@
package net.draycia.carbon.channels.contexts.impl;
import com.gmail.nossr50.api.PartyAPI;
import com.gmail.nossr50.events.party.McMMOPartyChangeEvent;
import net.draycia.carbon.CarbonChat;
import net.draycia.carbon.events.ChannelSwitchEvent;
import net.draycia.carbon.events.PreChatFormatEvent;
import net.draycia.carbon.storage.ChatUser;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public final class mcMMOContext implements Listener {
private CarbonChat carbonChat;
public mcMMOContext(CarbonChat carbonChat) {
this.carbonChat = carbonChat;
this.carbonChat.getContextManager().register("mcmmo-party", (context) -> {
if ((context.getValue() instanceof Boolean) && ((Boolean) context.getValue())) {
return isInSameParty(context.getSender(), context.getTarget());
}
return true;
});
}
@EventHandler(ignoreCancelled = true)
public void onChannelSwitch(ChannelSwitchEvent event) {
Object party = event.getChannel().getContext("mcmmo-party");
if ((party instanceof Boolean) && ((Boolean) party)) {
if (!isInParty(event.getUser())) {
event.setCancelled(true);
event.setFailureMessage(carbonChat.getConfig().getString("contexts.mcMMO.cancellation-message"));
}
}
}
@EventHandler(ignoreCancelled = true)
public void onChannelMessage(PreChatFormatEvent event) {
Object party = event.getChannel().getContext("mcmmo-party");
if ((party instanceof Boolean) && ((Boolean) party)) {
if (!isInParty(event.getUser())) {
event.setCancelled(true);
}
}
}
private final McMMOPartyChangeEvent.EventReason LEFT = McMMOPartyChangeEvent.EventReason.LEFT_PARTY;
private final McMMOPartyChangeEvent.EventReason KICKED = McMMOPartyChangeEvent.EventReason.KICKED_FROM_PARTY;
@EventHandler(ignoreCancelled = true)
public void onPartyLeave(McMMOPartyChangeEvent event) {
if (event.getReason() != LEFT && event.getReason() != KICKED) {
return;
}
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
Object party = user.getSelectedChannel().getContext("mcmmo-party");
if ((party instanceof Boolean) && ((Boolean) party)) {
user.clearSelectedChannel();
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
ChatUser user = carbonChat.getUserService().wrap(event.getPlayer());
Object party = user.getSelectedChannel().getContext("mcmmo-party");
if ((party instanceof Boolean) && ((Boolean) party) && !isInParty(user)) {
user.clearSelectedChannel();
}
}
public boolean isInParty(ChatUser user) {
return PartyAPI.inParty(user.asPlayer());
}
public boolean isInSameParty(ChatUser user1, ChatUser user2) {
if (!user1.isOnline() || !user2.isOnline()) {
return false;
}
return PartyAPI.inSameParty(user1.asPlayer(), user2.asPlayer());
}
}