Channel improvements and fixes

A chat event is no longer required for shouldSendTo() and such
TBMCPlugins/DiscordPlugin#70 is finally kind of fixed - there's a different issue now
This commit is contained in:
Norbi Peti 2018-11-01 01:20:49 +01:00
parent 5def9344e8
commit af412a40fd
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
9 changed files with 65 additions and 197 deletions

View file

@ -132,7 +132,7 @@ publish/
*.publishproj *.publishproj
# NuGet Packages Directory # NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line ## TO!DO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/ #packages/
# Windows Azure Build Output # Windows Azure Build Output

View file

@ -32,7 +32,7 @@ public class ScheduledRestartCommand extends TBMCCommandBase {
return false; return false;
} }
final int restarttime = restartcounter = ticks; final int restarttime = restartcounter = ticks;
restartbar = Bukkit.createBossBar("Server restart in " + ticks / 20f, BarColor.RED, BarStyle.SOLID, restartbar = Bukkit.createBossBar("Server restart in " + ticks / 20f + "s", BarColor.RED, BarStyle.SOLID,
BarFlag.DARKEN_SKY); BarFlag.DARKEN_SKY);
restartbar.setProgress(1); restartbar.setProgress(1);
Bukkit.getOnlinePlayers().forEach(p -> restartbar.addPlayer(p)); Bukkit.getOnlinePlayers().forEach(p -> restartbar.addPlayer(p));

View file

@ -1,53 +0,0 @@
package buttondevteam.lib;
import buttondevteam.lib.chat.Channel;
import lombok.Getter;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import javax.annotation.Nullable;
/**
* The purpose of this event is to determine which group the given channel belongs to
* or to validate that they have access to the given group chat.<br>
* It's not meant to be called from any plugin - it should be only created to use the helper methods
*/
@Getter
public class TBMCChannelConnectFakeEvent extends TBMCChatEventBase implements Cancellable {
private static final HandlerList handlers = new HandlerList();
@Nullable
private final CommandSender sender;
/**
* Using this the group will be determined based on the sender.
*
* @param sender The sender to get the group from
* @param channel The channel to use
*/
public TBMCChannelConnectFakeEvent(CommandSender sender, Channel channel) {
super(channel, "Channel connecting message. One of the things users should never see in action.", -1, null);
this.sender = sender;
}
/**
* Using this the given group will be validated and used.
*
* @param groupid The group to use, for example the name of a town or nation
* @param channel The channel to use
*/
public TBMCChannelConnectFakeEvent(String groupid, Channel channel) {
super(channel, "Channel connecting message. One of the things users should never see in action.", -1, groupid);
this.sender = null;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View file

@ -1,7 +1,6 @@
package buttondevteam.lib; package buttondevteam.lib;
import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -31,20 +30,14 @@ public abstract class TBMCChatEventBase extends Event implements Cancellable {
* Note: Errors are sent to the sender automatically * Note: Errors are sent to the sender automatically
*/ */
public boolean shouldSendTo(CommandSender sender) { public boolean shouldSendTo(CommandSender sender) {
if (channel.filteranderrormsg == null) return channel.shouldSendTo(sender, score);
return true;
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
return result.errormessage == null && score == result.score;
} }
/** /**
* Note: Errors are sent to the sender automatically * Note: Errors are sent to the sender automatically
*/ */
public int getMCScore(CommandSender sender) { public int getMCScore(CommandSender sender) {
if (channel.filteranderrormsg == null) return channel.getMCScore(sender);
return 0;
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
return result.errormessage == null ? result.score : -1;
} }
/** /**
@ -54,9 +47,6 @@ public abstract class TBMCChatEventBase extends Event implements Cancellable {
*/ */
@Nullable @Nullable
public String getGroupID(CommandSender sender) { public String getGroupID(CommandSender sender) {
if (channel.filteranderrormsg == null) return channel.getGroupID(sender); //TODO: Performance-wise it'd be much better to use serialization for player data - it's only converted once
return "everyone";
RecipientTestResult result = channel.filteranderrormsg.apply(sender);
return result.errormessage == null ? result.groupID : null;
} }
} }

View file

@ -14,6 +14,18 @@ import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
public class Channel { public class Channel {
/**
* Specifies a score that means it's OK to send - but it does not define any groups, only send or not send. See {@link #GROUP_EVERYONE}
*/
public static final int SCORE_SEND_OK = 0;
/**
* Specifies a score that means the user doesn't have permission to see or send the message. Any negative value has the same effect.
*/
public static final int SCORE_SEND_NOPE = -1;
/**
* Send the message to everyone <i>who has access to the channel</i> - this does not necessarily mean all players
*/
public static final String GROUP_EVERYONE = "everyone";
public final String DisplayName; public final String DisplayName;
public final Color color; public final Color color;
public final String ID; public final String ID;
@ -57,6 +69,43 @@ public class Channel {
this.filteranderrormsg = s -> filteranderrormsg.apply((T) this, s); this.filteranderrormsg = s -> filteranderrormsg.apply((T) this, s);
} }
public boolean isGlobal() {
return filteranderrormsg == null;
}
/**
* Note: Errors are sent to the sender automatically
*
* @param sender The user we're sending to
* @param score The (source) score to compare with the user's
*/
public boolean shouldSendTo(CommandSender sender, int score) {
return score == getMCScore(sender); //If there's any error, the score won't be equal
}
/**
* Note: Errors are sent to the sender automatically
*/
public int getMCScore(CommandSender sender) {
if (filteranderrormsg == null)
return SCORE_SEND_OK;
RecipientTestResult result = filteranderrormsg.apply(sender);
return result.errormessage == null ? result.score : SCORE_SEND_NOPE;
}
/**
* Note: Errors are sent to the sender automatically<br>
* <p>
* Null means don't send
*/
@Nullable
public String getGroupID(CommandSender sender) {
if (filteranderrormsg == null)
return GROUP_EVERYONE;
RecipientTestResult result = filteranderrormsg.apply(sender);
return result.errormessage == null ? result.groupID : null;
}
public static List<Channel> getChannels() { public static List<Channel> getChannels() {
return channels; return channels;
} }
@ -76,12 +125,12 @@ public class Channel {
public static Function<CommandSender, RecipientTestResult> noScoreResult(Predicate<CommandSender> filter, public static Function<CommandSender, RecipientTestResult> noScoreResult(Predicate<CommandSender> filter,
String errormsg) { String errormsg) {
return s -> filter.test(s) ? new RecipientTestResult(0, "everyone") : new RecipientTestResult(errormsg); return s -> filter.test(s) ? new RecipientTestResult(SCORE_SEND_OK, GROUP_EVERYONE) : new RecipientTestResult(errormsg);
} }
public static <T extends Channel> BiFunction<T, CommandSender, RecipientTestResult> noScoreResult( public static <T extends Channel> BiFunction<T, CommandSender, RecipientTestResult> noScoreResult(
BiPredicate<T, CommandSender> filter, String errormsg) { BiPredicate<T, CommandSender> filter, String errormsg) {
return (this_, s) -> filter.test(this_, s) ? new RecipientTestResult(0, "everyone") : new RecipientTestResult(errormsg); return (this_, s) -> filter.test(this_, s) ? new RecipientTestResult(SCORE_SEND_OK, GROUP_EVERYONE) : new RecipientTestResult(errormsg);
} }
public static Channel GlobalChat; public static Channel GlobalChat;
@ -95,7 +144,7 @@ public class Channel {
public static class RecipientTestResult { public static class RecipientTestResult {
public String errormessage = null; public String errormessage = null;
public int score = -1; // Anything below 0 is "never send" public int score = SCORE_SEND_NOPE; // Anything below 0 is "never send"
public String groupID = null; public String groupID = null;
/** /**
@ -110,7 +159,7 @@ public class Channel {
/** /**
* Creates a result that indicates a <b>success</b> * Creates a result that indicates a <b>success</b>
* *
* @param score The score that identifies the target group. For example, the index of the town or nation to send to. * @param score The score that identifies the target group. <b>Must be non-negative.</b> For example, the index of the town or nation to send to.
* @param groupID The ID of the target group. * @param groupID The ID of the target group.
*/ */
public RecipientTestResult(int score, String groupID) { public RecipientTestResult(int score, String groupID) {

View file

@ -20,6 +20,6 @@ public class ChannelPlayerData { //I just want this to work
} }
public void set(Channel value) { public void set(Channel value) {
data.set(value.toString()); data.set(value.ID);
} }
} }

View file

@ -206,119 +206,4 @@ public abstract class TBMCPlayerBase extends ChromaGamerBase {
if (keys.size() > 1) // PlayerName is always saved, but we don't need a file for just that if (keys.size() > 1) // PlayerName is always saved, but we don't need a file for just that
super.close(); super.close();
} }
/*private static void renameInTowny(TBMCPlayerBase player, Resident resident, String newName, TownyDatabaseHandler tdh) throws Exception {
val field=TownyDataSource.class.getDeclaredField("lock"); //TODO: Remove
field.setAccessible(true);
Lock lock=(Lock)field.get(tdh);
lock.lock(); //From Towny, removed the economy part, as that works by UUIDs
String oldName = resident.getName();
try {
String filteredName;
try {
filteredName = NameValidation.checkAndFilterName(newName);
} catch (InvalidNameException var39) {
throw new NotRegisteredException(var39.getMessage());
}
double balance = 0.0D;
Town town = null;
long registered = 0L;
long lastOnline = 0L;
boolean isMayor = false;
boolean isJailed = false;
int JailSpawn = 0;
//Don't do anything with economy balance as that works based on UUIDs
List<Resident> friends = resident.getFriends();
List<String> nationRanks = resident.getNationRanks();
TownyPermission permissions = resident.getPermissions();
String surname = resident.getSurname();
String title = resident.getTitle();
if (resident.hasTown()) {
town = resident.getTown();
}
List<TownBlock> townBlocks = resident.getTownBlocks();
List<String> townRanks = resident.getTownRanks();
registered = resident.getRegistered();
lastOnline = resident.getLastOnline();
isMayor = resident.isMayor();
isJailed = resident.isJailed();
int JailSpawn = resident.getJailSpawn();
this.deleteResident(resident);
this.universe.getResidentMap().remove(oldName.toLowerCase());
resident.setName(filteredName);
this.universe.getResidentMap().put(filteredName.toLowerCase(), resident);
if (transferBalance && TownySettings.isUsingEconomy()) {
try {
resident.setBalance(balance, "Rename Player - Transfer to new account");
} catch (EconomyException var37) {
var37.printStackTrace();
}
}
resident.setFriends(friends);
resident.setNationRanks(nationRanks);
resident.setPermissions(permissions.toString());
resident.setSurname(surname);
resident.setTitle(title);
resident.setTown(town);
resident.setTownblocks(townBlocks);
resident.setTownRanks(townRanks);
resident.setRegistered(registered);
resident.setLastOnline(lastOnline);
if (isMayor) {
try {
town.setMayor(resident);
} catch (TownyException var36) {
;
}
}
resident.setJailed(isJailed);
resident.setJailSpawn(JailSpawn);
this.saveResidentList();
this.saveResident(resident);
if (town != null) {
this.saveTown(town);
}
Iterator i$ = townBlocks.iterator();
while(i$.hasNext()) {
TownBlock tb = (TownBlock)i$.next();
this.saveTownBlock(tb);
}
Resident oldResident = new Resident(oldName);
List<Resident> toSaveResident = new ArrayList(this.getResidents());
Iterator i$ = toSaveResident.iterator();
Resident toCheck;
while(i$.hasNext()) {
toCheck = (Resident)i$.next();
if (toCheck.hasFriend(oldResident)) {
try {
toCheck.removeFriend(oldResident);
toCheck.addFriend(resident);
} catch (NotRegisteredException var35) {
var35.printStackTrace();
}
}
}
i$ = toSaveResident.iterator();
while(i$.hasNext()) {
toCheck = (Resident)i$.next();
this.saveResident(toCheck);
}
} finally {
this.lock.unlock();
}
BukkitTools.getPluginManager().callEvent(new RenameResidentEvent(oldName, resident));
this.universe.setChangedNotify(TownyObservableType.RENAME_RESIDENT);
}*/
} }

View file

@ -29,13 +29,4 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

View file

@ -28,6 +28,12 @@
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader> <!-- https://stackoverflow.com/a/53012553/2703239 -->
</configuration>
</plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>
</build> </build>