Skip to content

Commit

Permalink
Added global and team chats.
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMAVA committed Dec 26, 2019
1 parent 1a9e9fc commit eccfe08
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/me/PauMAVA/TTR/TTRCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package me.PauMAVA.TTR;

import me.PauMAVA.TTR.match.TTRMatch;
import me.PauMAVA.TTR.teams.TTRTeamHandler;
import me.PauMAVA.TTR.util.EventListener;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -26,14 +27,17 @@ public class TTRCore extends JavaPlugin {
private static TTRCore instance;
private boolean enabled = false;
private TTRMatch match;
private TTRTeamHandler teamHandler;

@Override
public void onEnable() {
instance = this;
if(this.getConfig().getBoolean("enableOnStart")) {
if (this.getConfig().getBoolean("enableOnStart")) {
enabled = true;
}
this.match = new TTRMatch();
this.teamHandler = new TTRTeamHandler();
this.teamHandler.setUpDefaultTeams();
this.getServer().getPluginManager().registerEvents(new EventListener(), this);
}

Expand All @@ -53,4 +57,8 @@ public boolean enabled() {
public TTRMatch getCurrentMatch() {
return this.match;
}

public TTRTeamHandler getTeamHandler() {
return this.teamHandler;
}
}
57 changes: 57 additions & 0 deletions src/me/PauMAVA/TTR/chat/TTRChatManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
*
* * TheTowersRemastered (TTR)
* * Copyright (c) 2019-2020 Pau Machetti Vallverdu
* *
* * This program is free software: you can redistribute it and/or modify
* * it under the terms of the GNU General Public License as published by
* * the Free Software Foundation, either version 3 of the License, or
* * (at your option) any later version.
* *
* * This program is distributed in the hope that it will be useful,
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* * GNU General Public License for more details.
* *
* * You should have received a copy of the GNU General Public License
* * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package me.PauMAVA.TTR.chat;

import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.teams.TTRTeam;
import me.PauMAVA.TTR.util.TTRPrefix;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class TTRChatManager {

public static void sendMessage(Player sender, String originalMessage) {
if(originalMessage.startsWith("!")) {
dispatchGlobalMessage(originalMessage);
} else {
dispatchTeamMessage(originalMessage, sender);
}
}

private static void dispatchGlobalMessage(String string) {
for(Player p: Bukkit.getServer().getOnlinePlayers()) {
p.sendMessage(TTRPrefix.TTR_GLOBAL + "" + ChatColor.GRAY + string);
}
}

private static void dispatchTeamMessage(String string, Player sender) {
TTRTeam playerTeam = TTRCore.getInstance().getTeamHandler().getPlayerTeam(sender);
if(playerTeam == null) {
return;
}
for(Player p: playerTeam.getPlayers()) {
p.sendMessage(TTRPrefix.TTR_TEAM + "" + ChatColor.GRAY + string);
}
}


}
9 changes: 9 additions & 0 deletions src/me/PauMAVA/TTR/teams/TTRTeamHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public boolean removePlayerFromTeam(Player player, String teamIdentifier) {
return true;
}

public TTRTeam getPlayerTeam(Player player) {
for(TTRTeam team: this.teams) {
if(team.getPlayers().contains(player)) {
return team;
}
}
return null;
}

private TTRTeam getTeam(String teamIdentifier) {
for(TTRTeam team: this.teams) {
if(team.getIdentifier().equalsIgnoreCase(teamIdentifier)) {
Expand Down
12 changes: 8 additions & 4 deletions src/me/PauMAVA/TTR/util/PacketIntercepter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.chat.TTRChatManager;
import net.minecraft.server.v1_15_R1.PacketPlayInChat;
import net.minecraft.server.v1_15_R1.PacketPlayOutChat;
import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;

public class PacketIntercepter {

public void addPlayer(Player player) {
ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler(){
@Override
public void channelRead(ChannelHandlerContext context, Object packet) {
if(packet instanceof PacketPlayOutChat) {

if(TTRCore.getInstance().enabled() && TTRCore.getInstance().getCurrentMatch().isOnCourse() && packet instanceof PacketPlayInChat && !((PacketPlayInChat) packet).b().startsWith("/")) {
TTRChatManager.sendMessage(player, ((PacketPlayInChat) packet).b());
}

try{
super.channelRead(context, packet);
} catch (Exception e) {

TTRCore.getInstance().getLogger().warning("An error occured on packet reading process!");
e.printStackTrace();
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/me/PauMAVA/TTR/util/TTRPrefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

public enum TTRPrefix {

TTR_GAME(ChatColor.GRAY + "" + ChatColor.BOLD + "[" + ChatColor.RESET + ChatColor.GREEN + "" + ChatColor.BOLD + "The Towers" + ChatColor.RESET + ChatColor.GRAY + "" + ChatColor.BOLD + "]" + ChatColor.RESET + " ");
TTR_GAME(ChatColor.GRAY + "" + ChatColor.BOLD + "[" + ChatColor.RESET + ChatColor.GREEN + "" + ChatColor.BOLD + "The Towers" + ChatColor.RESET + ChatColor.GRAY + "" + ChatColor.BOLD + "]" + ChatColor.RESET + " "),
TTR_GLOBAL(ChatColor.GRAY + "" + ChatColor.BOLD + "[" + ChatColor.RESET + ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "GLOBAL" + ChatColor.RESET + ChatColor.GRAY + "" + ChatColor.BOLD + "]" + ChatColor.RESET + " "),
TTR_TEAM(ChatColor.GRAY + "" + ChatColor.BOLD + "[" + ChatColor.RESET + ChatColor.GREEN + "" + ChatColor.BOLD + "TEAM" + ChatColor.RESET + ChatColor.GRAY + "" + ChatColor.BOLD + "]" + ChatColor.RESET + " ");

private String prefix;

Expand Down

0 comments on commit eccfe08

Please sign in to comment.