Skip to content

Instantly share code, notes, and snippets.

@jamietech
Last active December 16, 2015 11:38
Show Gist options
  • Save jamietech/5428587 to your computer and use it in GitHub Desktop.
Save jamietech/5428587 to your computer and use it in GitHub Desktop.
QuietJoin is a plugin requested by 'ebear' on the Bukkit forums. It mutes the user and blocks incoming chat to them until they say '/showchat'.

Copyright 2013 jamietech

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

name: QuietJoin
version: 1.0
author: jamietech
website: http://jamiete.ch
commands:
showchat:
usage: /<command>
description: Re-enables chat
package ch.jamiete.quietjoin;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
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;
import org.bukkit.plugin.java.JavaPlugin;
public class QuietJoin extends JavaPlugin implements Listener {
private final Set<String> usernames = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
private boolean onlyNew = true;
@EventHandler(priority = EventPriority.HIGHEST)
public void onAsyncPlayerChat(final AsyncPlayerChatEvent event) {
if (this.usernames.contains(event.getPlayer().getName())) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "Your chat is currently hidden. Please use /showchat to view chat and send messages.");
return;
}
for (final Player player : event.getRecipients()) {
if (this.usernames.contains(player.getName())) {
event.getRecipients().remove(player);
}
}
}
@Override
public void onEnable() {
if (!this.getConfig().isBoolean("onlynew")) {
this.getConfig().set("onlynew", true);
this.saveConfig();
this.getLogger().warning("First startup detected (or you deleted your config!). Set value `onlynew` to true (will only target FIRST JOINS).");
}
this.onlyNew = this.getConfig().getBoolean("onlynew", true);
this.getServer().getPluginManager().registerEvents(this, this);
this.getCommand("showchat").setExecutor(new CommandExecutor() {
@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
if (sender instanceof Player) {
if (QuietJoin.this.usernames.contains(sender.getName())) {
QuietJoin.this.usernames.remove(sender.getName());
sender.sendMessage(ChatColor.GREEN + "You can now see chat.");
}
} else {
sender.sendMessage(ChatColor.RED + "Only players may use that command.");
}
return true;
}
});
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(final PlayerJoinEvent event) {
if (this.onlyNew && !event.getPlayer().hasPlayedBefore()) {
return;
}
this.usernames.add(event.getPlayer().getName());
event.getPlayer().sendMessage(ChatColor.GREEN + "Welcome! Please use /showchat to view chat.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment