Created
November 5, 2016 22:16
-
-
Save thomas15v/13466fbec4aeb0d82362d65e61ea6316 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2014 Florian Stober | |
* | |
* 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 codecrafter47.freebungeechat.server; | |
/** | |
* @author Florian Stober | |
*/ | |
public class ChannelConstants { | |
public final static String channel = "FreeBungeeChat"; | |
public final static String subchannel_playSound = "playsound"; | |
public final static String subchannel_chatMsg = "chat"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package codecrafter47.freebungeechat.server.sponge; | |
import codecrafter47.freebungeechat.server.ChannelConstants; | |
import com.google.inject.Inject; | |
import lombok.SneakyThrows; | |
import org.spongepowered.api.Game; | |
import org.spongepowered.api.Platform; | |
import org.spongepowered.api.effect.sound.SoundType; | |
import org.spongepowered.api.entity.living.player.Player; | |
import org.spongepowered.api.event.Listener; | |
import org.spongepowered.api.event.game.state.GameStartedServerEvent; | |
import org.spongepowered.api.network.ChannelBinding; | |
import org.spongepowered.api.network.PlayerConnection; | |
import org.spongepowered.api.plugin.Plugin; | |
@Plugin(id="freebungeechat", name = "FreeBungeeChat", version = "1.0.0") | |
public class FreeBungeeChatSponge { | |
@Inject | |
private Game game; | |
private ChannelBinding.RawDataChannel channel; | |
@Listener | |
public void onConstructionEvent(GameStartedServerEvent event) { | |
channel = game.getChannelRegistrar().createRawChannel(this, ChannelConstants.channel); | |
channel.addListener(Platform.Type.SERVER, (buf, con, side) -> { | |
String messageType = buf.readUTF(); | |
if (con instanceof PlayerConnection) { | |
Player player = ((PlayerConnection) con).getPlayer(); | |
if (ChannelConstants.subchannel_playSound.equalsIgnoreCase(messageType)) { | |
player.playSound(SoundType.of(buf.readUTF()), player.getLocation().getPosition(), 5); | |
} else if (ChannelConstants.subchannel_chatMsg.equalsIgnoreCase(messageType)){ | |
String text = buf.readUTF(); | |
String prefix = buf.readUTF(); | |
int id = buf.readInteger(); | |
boolean allowBBCode = buf.readBoolean(); | |
processChatMessage(player, text, prefix, id, allowBBCode); | |
} | |
} | |
}); | |
} | |
@SneakyThrows | |
private void processChatMessage(Player player, String text, String prefix, int id, boolean allowBBCode) { | |
/*if (text.contains("%" + prefix + "group%")) { | |
text = text.replace("%" + prefix + "group%", wrapVariable(vaultHook.getGroup(player), allowBBCode)); | |
}*/ | |
if (text.contains("%" + prefix + "prefix%")) { | |
text = text.replace("%" + prefix + "prefix%", wrapVariable(player.getOption("prefix").orElse(""), allowBBCode)); | |
} | |
if (text.contains("%" + prefix + "prefixcolor%")) { | |
String prefix1 = player.getOption("suffix").orElse(""); | |
text = text.replace("%" + prefix + "prefixcolor%", wrapVariable(prefix1.substring(0, Math.min(2, prefix1.length())), allowBBCode)); | |
} | |
if (text.contains("%" + prefix + "suffix%")) { | |
text = text.replace("%" + prefix + "suffix%", wrapVariable(player.getOption("prefix").orElse(""), allowBBCode)); | |
} | |
/*if (text.contains("%" + prefix + "balance%")) { | |
text = text.replace("%" + prefix + "balance%", wrapVariable(vaultHook.getBalance(player), allowBBCode)); | |
} | |
if (text.contains("%" + prefix + "currency%")) { | |
text = text.replace("%" + prefix + "currency%", wrapVariable(vaultHook.getCurrencyName(), allowBBCode)); | |
} | |
if (text.contains("%" + prefix + "currencyPl%")) { | |
text = text.replace("%" + prefix + "currencyPl%", wrapVariable(vaultHook.getCurrencyNamePl(), allowBBCode)); | |
}*/ | |
/*if (text.contains("%" + prefix + "tabName%")) { | |
text = text.replace("%" + prefix + "tabName%", wrapVariable(player.getPlayerListName(), allowBBCode)); | |
} | |
if (text.contains("%" + prefix + "displayName%")) { | |
text = text.replace("%" + prefix + "displayName%", wrapVariable(player.getDisplayName(), allowBBCode)); | |
}*/ | |
if (text.contains("%" + prefix + "world%")) { | |
text = text.replace("%" + prefix + "world%", wrapVariable(player.getWorld().getName(), allowBBCode)); | |
} | |
/*if (text.contains("%" + prefix + "health%")) { | |
text = text.replace("%" + prefix + "health%", wrapVariable(Double.toString(player.getHealth()), allowBBCode)); | |
} | |
if (text.contains("%" + prefix + "level%")) { | |
text = text.replace("%" + prefix + "level%", wrapVariable(Integer.toString(player.getLevel()), allowBBCode)); | |
}*/ | |
String finalText = text; | |
channel.sendTo(player, channelBuf -> { | |
channelBuf.writeUTF(ChannelConstants.subchannel_chatMsg); | |
channelBuf.writeInteger(id); | |
channelBuf.writeUTF(finalText); | |
}); | |
} | |
public String wrapVariable(String variable, boolean allowBBCode) { | |
//variable = LEGACY_FORMATTING_CODE.serialize(TEXT_XML.deserialize(variable)).replace("§", "&");; | |
if (allowBBCode) { | |
return variable; | |
} else { | |
return "[nobbcode]" + variable + "[/nobbcode]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you install this to a sponge server to get it to work with FreeBungeeChat?