Skip to content

Commit

Permalink
Enables teamchat to exist outside of one game world
Browse files Browse the repository at this point in the history
A default game mode can be defined in the config to allow chatting
outside of that game world.

#33
  • Loading branch information
tastybento committed Sep 14, 2021
1 parent cd58d9e commit a7f0fe3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.1.5</build.version>
<build.version>1.2.0</build.version>
</properties>

<!-- Profiles will allow to automatically change build version. -->
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/world/bentobox/chat/Chat.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world.bentobox.chat;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -29,6 +30,7 @@ public class Chat extends Addon {
private Config<Settings> configObject = new Config<>(this, Settings.class);
private Set<GameModeAddon> registeredGameModes;
private ChatListener listener;
private Optional<World> chatWorld;

@Override
public void onEnable() {
Expand All @@ -45,7 +47,10 @@ public void onEnable() {

/* Setup */
setupCommands();

// Save default world
chatWorld = getPlugin().getAddonsManager().getGameModeAddons().stream()
.filter(gm -> settings.getDefaultChatGamemode().equalsIgnoreCase(gm.getDescription().getName()))
.findFirst().map(GameModeAddon::getOverWorld);
// Register listener
listener = new ChatListener(this);
// This manually registers the AsyncPlayerChatEvent with the priority from configuration
Expand Down Expand Up @@ -125,4 +130,11 @@ public ChatListener getListener() {
public void setListener(ChatListener listener) {
this.listener = listener;
}

/**
* @return the chatWorld
*/
public Optional<World> getChatWorld() {
return chatWorld;
}
}
29 changes: 28 additions & 1 deletion src/main/java/world/bentobox/chat/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
@ConfigComment("Configuration file for Chat [version].")
public class Settings implements ConfigObject {

@ConfigComment("Lists the gamemodes in which you want the Team Chat to be effective.")
@ConfigEntry(path = "team-chat.gamemodes")
private List<String> teamChatGamemodes = Arrays.asList("BSkyBlock", "AcidIsland", "CaveBlock", "SkyGrid");

@ConfigComment("If players are outside a game world, team chat can still exist for one game mode. List it")
@ConfigComment("here if you want that, e.g., BSkyBlock")
@ConfigEntry(path = "team-chat.default-teamchat-gamemode")
private String defaultChatGamemode = "";

@ConfigComment("Log team chats to console.")
@ConfigEntry(path = "team-chat.log")
private boolean logTeamChats;

@ConfigComment("Lists the gamemodes in which you want the Chat addon to be effective.")
@ConfigComment("Lists the gamemodes in which you want the Island Chat to be effective.")
@ConfigEntry(path = "island-chat.gamemodes")
private List<String> islandChatGamemodes = Arrays.asList("BSkyBlock", "AcidIsland", "CaveBlock", "SkyGrid");

Expand Down Expand Up @@ -100,4 +106,25 @@ public EventPriority getEventPriority() {
public void setEventPriority(EventPriority eventPriority) {
this.eventPriority = eventPriority.toString();
}

/**
* @return the universaleChatGamemode
*/
public String getDefaultChatGamemode() {
return defaultChatGamemode;
}

/**
* @param universaleChatGamemode the universaleChatGamemode to set
*/
public void setDefaultChatGamemode(String universaleChatGamemode) {
this.defaultChatGamemode = universaleChatGamemode;
}

/**
* @param eventPriority the eventPriority to set
*/
public void setEventPriority(String eventPriority) {
this.eventPriority = eventPriority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean execute(User user, String label, List<String> args) {
// Send the message directly into team chat without the need of toggling it
// if there is existence of more arguments
if (!args.isEmpty()) {
addon.getListener().teamChat(user.getPlayer(), String.join(" ", args));
addon.getListener().teamChat(getWorld(), user.getPlayer(), String.join(" ", args));
return true;
}

Expand Down
21 changes: 14 additions & 7 deletions src/main/java/world/bentobox/chat/listeners/ChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public ChatListener(Chat addon) {
islandSpies = new HashSet<>();
}

@Override
public void execute(Listener listener, Event e) {

// Needs to be checked, as we manually registered the listener
Expand All @@ -61,18 +62,24 @@ public void execute(Listener listener, Event e) {
public void onChat(final AsyncPlayerChatEvent e) {

Player p = e.getPlayer();
World w = e.getPlayer().getWorld();
World ww = e.getPlayer().getWorld();
// Check world
if (!addon.isRegisteredGameWorld(w)) {
return;
if (!addon.isRegisteredGameWorld(ww)) {
// Check to see if there is a default game mode for chat
if (addon.getChatWorld().isPresent()) {
ww = addon.getChatWorld().get();
} else {
return;
}
}
World w = ww;
if (teamChatUsers.contains(p.getUniqueId()) && addon.getIslands().inTeam(w, p.getUniqueId())) {
// Cancel the event
e.setCancelled(true);
if (e.isAsynchronous()) {
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> teamChat(p,e.getMessage()));
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> teamChat(w, p, e.getMessage()));
} else {
teamChat(p, e.getMessage());
teamChat(w, p, e.getMessage());
}
}
addon.getIslands().getIslandAt(p.getLocation())
Expand Down Expand Up @@ -121,9 +128,9 @@ public void islandChat(Island i, Player player, String message) {
.forEach(a -> a.sendMessage("chat.island-chat.spy.syntax", TextVariables.NAME, player.getName(), MESSAGE, message));
}

public void teamChat(final Player player, String message) {
public void teamChat(World w, final Player player, String message) {
// Get island members of member or above
addon.getIslands().getIsland(player.getWorld(), player.getUniqueId()).getMemberSet().stream()
addon.getIslands().getIsland(w, player.getUniqueId()).getMemberSet().stream()
// Map to users
.map(User::getInstance)
// Filter for online only
Expand Down
10 changes: 7 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# Configuration file for Chat
# Configuration file for Chat 1.2.0-SNAPSHOT-LOCAL.
team-chat:
# Lists the gamemodes in which you want the Team Chat to be effective.
gamemodes:
- BSkyBlock
- AcidIsland
- CaveBlock
- SkyGrid
# If players are outside a game world, team chat can still exist for one game mode. List it
# here if you want that, e.g., BSkyBlock
default-teamchat-gamemode: ''
# Log team chats to console.
log: false
island-chat:
# Lists the gamemodes in which you want the Chat addon to be effective.
# Lists the gamemodes in which you want the Island Chat to be effective.
gamemodes:
- BSkyBlock
- AcidIsland
Expand All @@ -20,4 +24,4 @@ chat-listener:
# Sets priority of AsyncPlayerChatEvent. Change this if Chat addon
# is conflicting with other plugins which listen to the same event
# Acceptable values: lowest, low, normal, high, highest, monitor
priority: normal
priority: NORMAL

0 comments on commit a7f0fe3

Please sign in to comment.