From 4e279fae2e7405947c28a2ffb3f05afe8e774333 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Sun, 17 Nov 2013 15:26:28 +0100 Subject: [PATCH] Distort Certain Commands --- config.yml | 32 ++++++++--- src/com/dre/brewery/P.java | 1 + src/com/dre/brewery/Words.java | 53 +++++++++++++++++-- .../dre/brewery/listeners/PlayerListener.java | 9 ++++ 4 files changed, 83 insertions(+), 12 deletions(-) diff --git a/config.yml b/config.yml index b1358aa..9f32cd9 100644 --- a/config.yml +++ b/config.yml @@ -1,7 +1,7 @@ # config for Brewery.jar -# Settings +# -- Settings -- # Defaults are written in [] # Deleting of single settings disables them @@ -36,7 +36,7 @@ enablePuke: true # Item that is dropped multiple times uncollectable when puking [SOUL_SAND] pukeItem: SOUL_SAND -# Consumable Item/strength. Decreases the alcohol level by when consumed. +# Consumable Item/strength. Decreases the alcohol level by when consumed. (list) drainItems: - BREAD/4 - MILK_BUCKET/2 @@ -48,17 +48,14 @@ hangoverDays: 7 colorInBarrels: true colorInBrewer: false -# Log to the Serverlog what the player actually wrote, before his words were altered [false] -logRealChat: false - # Autosave interval in minutes [3] autosave: 3 # Config Version -version: '1.0' +version: '1.1' -# Recipes for Potions +# -- Recipes for Potions -- # name: Different names for bad/normal/good (Formatting codes possible: such as &6) # ingredients: List of material/amount @@ -194,6 +191,27 @@ cooked: +# -- Chat Distortion Settings -- + +# Log to the Serverlog what the player actually wrote, before his words were altered [false] +logRealChat: false + +# Text after specified commands will be distorted when drunk (list) [- /gl] +distortCommands: +- /gl +- /global +- /fl +- /s +- /letter + +# Distort the Text written on a Sign while drunk [false] +distortSignText: false + +# Enclose a text with these Letters to bypass Chat Distortion (Use "," as Seperator) (list) [- '[,]'] +distortBypass: +- '*,*' +- '[,]' + # words: Words and letters that will be altered when chatting while being drunk. # Will be processed from first to last and a written sentece is altered in that order. diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index 98c6318..9bdf5c4 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -184,6 +184,7 @@ public class P extends JavaPlugin { Brew.colorInBarrels = config.getBoolean("colorInBarrels", false); Brew.colorInBrewer = config.getBoolean("colorInBrewer", false); Words.log = config.getBoolean("logRealChat", false); + Words.commands = config.getStringList("distortCommands"); // loading recipes ConfigurationSection configSection = config.getConfigurationSection("recipes"); diff --git a/src/com/dre/brewery/Words.java b/src/com/dre/brewery/Words.java index 201ff6f..9a7a989 100644 --- a/src/com/dre/brewery/Words.java +++ b/src/com/dre/brewery/Words.java @@ -1,10 +1,14 @@ package com.dre.brewery; +import java.util.List; import java.util.ArrayList; +import java.util.HashMap; import java.util.Map; +import java.lang.Character; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; import com.dre.brewery.BPlayer; @@ -13,8 +17,10 @@ public class Words { // represends Words and letters, that are replaced in drunk players messages public static ArrayList words = new ArrayList(); + public static List commands; public static FileConfiguration config; public static Boolean log; + private static Map waitPlayers = new HashMap(); private String from; private String to; @@ -59,15 +65,52 @@ public class Words { } } + private static boolean loadWords() { + if (words.isEmpty()) { + // load when first drunk player talks + load(); + } + return !words.isEmpty(); + } + + // Distort players words when he uses a command + public static void playerCommand(PlayerCommandPreprocessEvent event) { + String name = event.getPlayer().getName(); + BPlayer bPlayer = BPlayer.get(name); + if (bPlayer != null) { + if (!commands.isEmpty() && loadWords()) { + if (!waitPlayers.containsKey(name) || waitPlayers.get(name) + 500 < System.currentTimeMillis()) { + String chat = event.getMessage(); + for (String command : commands) { + if (command.length() + 1 < chat.length()) { + if (Character.isSpaceChar(chat.charAt(command.length()))) { + if (chat.toLowerCase().startsWith(command.toLowerCase())) { + if (log) { + P.p.log(P.p.languageReader.get("Player_TriedToSay", name, chat)); + } + String message = chat.substring(command.length() + 1); + for (Words word : words) { + if (word.alcohol <= bPlayer.getDrunkeness()) { + message = word.distort(message); + } + } + event.setMessage(chat.substring(0, command.length() + 1) + message); + waitPlayers.put(name, System.currentTimeMillis()); + return; + } + } + } + } + } + } + } + } + // Distort players words when he talks public static void playerChat(AsyncPlayerChatEvent event) { BPlayer bPlayer = BPlayer.get(event.getPlayer().getName()); if (bPlayer != null) { - if (words.isEmpty()) { - // load when first drunk player talks - load(); - } - if (!words.isEmpty()) { + if (loadWords()) { String message = event.getMessage(); if (log) { P.p.log(P.p.languageReader.get("Player_TriedToSay", event.getPlayer().getName(), message)); diff --git a/src/com/dre/brewery/listeners/PlayerListener.java b/src/com/dre/brewery/listeners/PlayerListener.java index 116fc9a..498cfe3 100644 --- a/src/com/dre/brewery/listeners/PlayerListener.java +++ b/src/com/dre/brewery/listeners/PlayerListener.java @@ -6,6 +6,7 @@ import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.EventHandler; @@ -165,6 +166,14 @@ public class PlayerListener implements Listener { Words.playerChat(event); } } + + // player commands while drunk, distort chat commands + @EventHandler(priority = EventPriority.LOWEST) + public void onCommandPreProcess(PlayerCommandPreprocessEvent event) { + if (BPlayer.players.containsKey(event.getPlayer().getName())) { + Words.playerCommand(event); + } + } // player joins while passed out @EventHandler(priority = EventPriority.LOW)