Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shevchik committed Jul 4, 2014
0 parents commit f8169c6
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Build Folders #
bin/
build/
target/
.externalToolBuilders/
# Eclipse Files #
*.project
*.classpath
*.prefs
68 changes: 68 additions & 0 deletions src/tgchatchannels/ChannelData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

package tgchatchannels;

import java.util.HashSet;
import java.util.UUID;

public class ChannelData {

private UUID owner;
private boolean privateChannel = true;
private HashSet<UUID> players = new HashSet<UUID>();

protected ChannelData() {
}

public ChannelData(UUID owner) {
this.owner = owner;
}

public UUID getOwner() {
return owner;
}

public boolean isOwner(UUID uuid) {
return owner != null ? owner.equals(uuid) : false;
}

public boolean isPrivate() {
return privateChannel;
}

public void setPrivate() {
privateChannel = true;
}

public void setPublic() {
privateChannel = false;
}

public boolean isInChannel(UUID player) {
return players.contains(player);
}

public void addPlayer(UUID player) {
players.add(player);
}

public void removePlayer(UUID player) {
players.remove(player);
}

}
125 changes: 125 additions & 0 deletions src/tgchatchannels/ChannelsStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

package tgchatchannels;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

public class ChannelsStorage {

private TGChatChannels plugin;

public ChannelsStorage(TGChatChannels plugin) {
this.plugin = plugin;
}

private HashMap<String, ChannelData> channels = new HashMap<String, ChannelData>();
private HashSet<String> defaultChannels = new HashSet<String>();
private HashMap<UUID, PlayerData> playersData = new HashMap<UUID, PlayerData>();

public void loadDefaultChannels() {
File configfile = new File(plugin.getDataFolder(), "config.yml");
FileConfiguration config = YamlConfiguration.loadConfiguration(configfile);
defaultChannels.addAll(config.getStringList("defaultchannels"));
config = new YamlConfiguration();
config.set("defaultchannels", new ArrayList<String>(defaultChannels));
try {
config.save(configfile);
} catch (IOException e) {
}
}

public void load() {
File configfile = new File(plugin.getDataFolder(), "data.yml");
FileConfiguration config = YamlConfiguration.loadConfiguration(configfile);
ConfigurationSection channels = config.getConfigurationSection("channels");
if (channels != null) {
for (String channelName : channels.getKeys(false)) {
ChannelData data = null;
String uuidstring = channels.getString(channelName+".owner", null);
if (uuidstring == null) {
data = new ChannelData();
} else {
data = new ChannelData(UUID.fromString(uuidstring));
}
boolean privateChannel = channels.getBoolean(channelName+".private", false);
if (!privateChannel) {
data.setPublic();
}
for (String playerUUIDString : channels.getStringList(channelName+".players")) {
data.addPlayer(UUID.fromString(playerUUIDString));
}
}
}
ConfigurationSection players = config.getConfigurationSection("players");
if (players != null) {
for (String playerUUIDString : players.getKeys(false)) {
UUID uuid = UUID.fromString(playerUUIDString);
PlayerData data = new PlayerData();
data.setCurrentChannel(players.getString(playerUUIDString+".channel"));
playersData.put(uuid, data);
}
}
}

public void save() {

}

public PlayerData getPlayerData(UUID playerUUID) {
return playersData.get(playerUUID);
}

public void addToDefaultChannels(UUID uuid) {
PlayerData data = new PlayerData();
if (!defaultChannels.isEmpty()) {
data.setCurrentChannel(defaultChannels.iterator().next());
}
playersData.put(uuid, data);
for (String channelName : defaultChannels) {
if (channels.containsKey(channelName)) {
channels.get(channelName).addPlayer(uuid);
}
}
}

public boolean channelExists(String channelName) {
return channels.containsKey(channelName);
}

public ChannelData getChannelData(String channelName) {
return channels.get(channelName);
}

public void addChannel(UUID owner, String channelName) {
channels.put(channelName, new ChannelData(owner));
}

public void removeChannel(String channelName) {
channels.remove(channelName);
}

}
68 changes: 68 additions & 0 deletions src/tgchatchannels/ChatListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

package tgchatchannels;

import java.util.Iterator;
import java.util.UUID;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;

public class ChatListener implements Listener {

private ChannelsStorage storage;

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onAsyncChat(AsyncPlayerChatEvent event) {
UUID playerUUID = event.getPlayer().getUniqueId();
PlayerData data = storage.getPlayerData(playerUUID);
if (data.getCurrentChannel() == null) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "Вы не можете говорить в несуществующий канал");
return;
}
String channelName = data.getCurrentChannel();
if (!storage.channelExists(channelName)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "Вы не можете говорить в несуществующий канал");
return;
}
event.setFormat("["+channelName+"] "+event.getFormat());
ChannelData currentChannel = storage.getChannelData(channelName);
Iterator<Player> recipientsIt = event.getRecipients().iterator();
while (recipientsIt.hasNext()) {
UUID uuid = recipientsIt.next().getUniqueId();
if (!currentChannel.isInChannel(uuid)) {
recipientsIt.remove();
}
}
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
if (!event.getPlayer().hasPlayedBefore()) {
storage.addToDefaultChannels(event.getPlayer().getUniqueId());
}
}

}
22 changes: 22 additions & 0 deletions src/tgchatchannels/Commands.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

package tgchatchannels;

public class Commands {

}
32 changes: 32 additions & 0 deletions src/tgchatchannels/PlayerData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

package tgchatchannels;

public class PlayerData {

private String currentChannel;

public String getCurrentChannel() {
return currentChannel;
}

public void setCurrentChannel(String channelName) {
this.currentChannel = channelName;
}

}
24 changes: 24 additions & 0 deletions src/tgchatchannels/TGChatChannels.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/

package tgchatchannels;

import org.bukkit.plugin.java.JavaPlugin;

public class TGChatChannels extends JavaPlugin {

}

0 comments on commit f8169c6

Please sign in to comment.