diff --git a/config.yml b/config.yml index dd66316..2534fec 100644 --- a/config.yml +++ b/config.yml @@ -10,6 +10,13 @@ #cooked: Auflistung ALLER möglichen Zutaten und die daraus entstehenden Tranknamen: (leer für undef.) +#words: Wörter und buchstaben die bei trunkenheit ersetzt werden sollen +#to: in welches wort es ersetzt werden soll +#pre: Wörter und Buchstaben vor dem gesuchten wort +#match: true = eines der "pre"-Wörter muss vor dem gesuchten Wort stehen, false = keines der "pre" Wörter darf vor dem gesuchten stehen +#alcohol: 1-100 trunkenheit ab der die wörter ersetzt werden +#percentage: Wahrscheinlichkeit des Ersetzen eines wortes in prozent + recipes: 1: @@ -92,4 +99,21 @@ cooked: SUGAR_CANE: Zuckersud APPLE: Apfelmost POTATO_ITEM: Kartoffelmaische - LONG_GRASS: Kräuterbrühe \ No newline at end of file + LONG_GRASS: Kräuterbrühe +words: + ch: + to: sch + pre: u,s,o,a + match: false + alcohol: 20 + percentage: 70 + h: + to: hh + percentage: 60 + u: + to: uuh + percentage: 20 + das: + to: dass + percentage: 10 + alcohol: 40 \ No newline at end of file diff --git a/src/com/dre/brewery/BPlayer.java b/src/com/dre/brewery/BPlayer.java index f2f17bb..f9ffae1 100644 --- a/src/com/dre/brewery/BPlayer.java +++ b/src/com/dre/brewery/BPlayer.java @@ -6,7 +6,6 @@ import java.util.HashMap; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.entity.Player; import org.bukkit.entity.Entity; -import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.util.Vector; import com.dre.brewery.Brew; @@ -37,8 +36,8 @@ public class BPlayer { }*/ //returns true if drinking was successful - public static boolean drink(PotionMeta meta,Player player){ - Brew brew = Brew.get(meta); + public static boolean drink(int uid,Player player){ + Brew brew = Brew.get(uid); if(brew != null){ BPlayer bPlayer = get(player); if(bPlayer == null){ diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index 69bd659..a5d96ed 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -48,13 +48,56 @@ public class Brew { potions.put(uid,this); } + //returns a Brew by its UID + public static Brew get(int uid){ + if(uid < -1){ + if(!potions.containsKey(uid)){ + P.p.errorLog("Database failure! unable to find UID "+uid+" of a custom Potion!"); + return null;//throw some exception? + } + } else { + return null; + } + return potions.get(uid); + } + + //returns a Brew by PotionMeta + public static Brew get(PotionMeta meta){ + return get(getUID(meta)); + } + + //returns a Brew by ItemStack + /*public static Brew get(ItemStack item){ + if(item.getTypeId() == 373){ + PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); + return get(potionMeta); + } + return null; + }*/ + + //returns UID of custom Potion item + public static int getUID(ItemStack item){ + return getUID((PotionMeta) item.getItemMeta()); + } + + + //returns UID of custom Potion meta + public static int getUID(PotionMeta potionMeta){ + if(potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)){ + for(PotionEffect effect:potionMeta.getCustomEffects()){ + if(effect.getType().equals(PotionEffectType.REGENERATION)){ + if(effect.getDuration() < -1){ + return effect.getDuration(); + } + } + } + } + return 0; + } + //remove potion from map (drinking, despawning, should be more!) public static void remove(ItemStack item){ - PotionMeta meta = (PotionMeta) item.getItemMeta(); - Brew brew = get(meta); - if(brew != null){ - potions.remove(brew); - } + potions.remove(getUID(item)); } @@ -67,41 +110,6 @@ public class Brew { return uid; } - //returns a Brew by its UID - public static Brew getByUID(int uid){ - if(uid < -1){ - if(!potions.containsKey(uid)){ - P.p.errorLog("Database failure! unable to find UID "+uid+" of a custom Potion!"); - return null;//throw some exception? - } - } else { - return null; - } - return potions.get(uid); - - } - - //returns a Brew by PotionMeta - public static Brew get(PotionMeta meta){ - if(meta.hasCustomEffects()){ - for(PotionEffect effect:meta.getCustomEffects()){ - if(effect.getType().equals(PotionEffectType.REGENERATION)){ - return getByUID(effect.getDuration()); - } - } - } - return null; - } - - //returns a Brew by ItemStack - /*public static Brew get(ItemStack item){ - if(item.getTypeId() == 373){ - PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); - return get(potionMeta); - } - return null; - }*/ - //calculate alcohol from recipe public int calcAlcohol(int alc){ if(distillRuns == 0){ diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index cd6030c..ba17fe4 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -104,6 +104,14 @@ public class P extends JavaPlugin{ } } + //telling Words the path, it will load it when needed + configSection = config.getConfigurationSection("words"); + if(configSection != null){ + if(!configSection.getKeys(false).isEmpty()){ + Words.config = configSection; + } + } + } //load all Data diff --git a/src/com/dre/brewery/Words.java b/src/com/dre/brewery/Words.java new file mode 100644 index 0000000..2fab35f --- /dev/null +++ b/src/com/dre/brewery/Words.java @@ -0,0 +1,135 @@ +package com.dre.brewery; + +import java.util.ArrayList; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.event.player.AsyncPlayerChatEvent; + +import com.dre.brewery.BPlayer; + +public class Words { + + + private static ArrayList words = new ArrayList();//material and amount + public static ConfigurationSection config; + + private String from; + private String to; + private String[] pre; + private Boolean match; + private int alcohol; + private int percentage; + + public Words(ConfigurationSection section){ + if(section.getString("to",null) != null){ + this.from = section.getName(); + this.to = section.getString("to",null); + String pre = section.getString("pre",null); + if(pre != null){ + this.pre = pre.split(","); + } + this.match = section.getBoolean("match",false); + this.alcohol = section.getInt("alcohol",10); + this.percentage = section.getInt("percentage",100); + words.add(this); + } + } + + //Distort players words when he talks + public static void playerChat(AsyncPlayerChatEvent event){ + BPlayer bPlayer = BPlayer.get(event.getPlayer()); + if(bPlayer != null){ + if(words.isEmpty()){ + load(); + } + if(!words.isEmpty()){ + String message = event.getMessage(); + for(Words w:words){ + if(w.alcohol <= bPlayer.getDrunkeness()){ + message = distort(message, w.from, w.to, w.pre, w.match, w.percentage); + } + } + event.setMessage(message); + } + } + } + + //replace "percent"% of "from" -> "to" in "words", when the string before each "from" "match"es "pre" + //Not yet ignoring case :( + public static String distort(String words, String from, String to, String[] pre, boolean match, int percent){ + if(words.contains(from)){ + if(pre == null && percent == 100){ + //All occurences of "from" need to be replaced + return words.replaceAll(from,to); + } + String newWords = ""; + if(words.endsWith(from)){ + //add space to end to recognize last occurence of "from" + words = words+" "; + } + //remove all "from" and split "words" there + String[] splitted = words.split(from); + int index = 0; + String part = null; + boolean isBefore = !match; + + //if there are occurences of "from" + if(splitted.length > 1){ + //- 1 because dont add "to" to the end of last part + while(index < splitted.length - 1){ + part = splitted[index]; + //add current part of "words" to the output + newWords = newWords+part; + //check if the part ends with correct string + if(pre != null){ + for(String pr:pre){ + if(match == true){ + //if one is correct, it is enough + if(part.endsWith(pr) == match){ + isBefore = true; + break; + } + } else { + //if one is wrong, its over + if(part.endsWith(pr) != match){ + isBefore = false; + break; + } + } + } + } else { + isBefore = true; + } + if(isBefore && Math.random() * 100.0 <= percent){ + //add replacement + newWords = newWords+to; + } else { + //add original + newWords = newWords+from; + } + index++; + } + //add the last part to finish the sentence + part = splitted[index]; + if(part.equals(" ")){ + //dont add the space to the end + return newWords; + } else { + return newWords + part; + } + } + } + return words; + } + + //loaded when first drunken player speaks + public static void load(){ + if(config != null){ + for(String word:config.getKeys(false)){ + new Words(config.getConfigurationSection(word)); + } + } + } + + +} \ No newline at end of file diff --git a/src/com/dre/brewery/listeners/PlayerListener.java b/src/com/dre/brewery/listeners/PlayerListener.java index ab37f06..cbb2d10 100644 --- a/src/com/dre/brewery/listeners/PlayerListener.java +++ b/src/com/dre/brewery/listeners/PlayerListener.java @@ -4,6 +4,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.inventory.BrewEvent; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -15,15 +16,12 @@ import org.bukkit.inventory.BrewerInventory; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.potion.PotionEffectType; -import org.bukkit.potion.PotionEffect; - import com.dre.brewery.BCauldron; import com.dre.brewery.BIngredients; import com.dre.brewery.Brew; import com.dre.brewery.Barrel; import com.dre.brewery.BPlayer; +import com.dre.brewery.Words; public class PlayerListener implements Listener{ @EventHandler(priority = EventPriority.HIGH) @@ -107,13 +105,10 @@ public class PlayerListener implements Listener{ if(item != null){ if(item.getType() == Material.POTION){ if(item.hasItemMeta()){ - PotionMeta potionMeta = ((PotionMeta) item.getItemMeta()); - if(potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)){ - if(Brew.get(potionMeta) != null){ - //has custom potion in "slot" - contents[slot] = 1; - custom = true; - } + if(Brew.potions.containsKey(Brew.getUID(item))){ + //has custom potion in "slot" + contents[slot] = 1; + custom = true; } } } @@ -134,15 +129,9 @@ public class PlayerListener implements Listener{ if(item != null){ if(item.getType() == Material.POTION){ if(item.hasItemMeta()){ - PotionMeta potionMeta = ((PotionMeta) item.getItemMeta()); - if(potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)){ - for(PotionEffect effect:potionMeta.getCustomEffects()){ - if(effect.getType().getId() == 10){ - if(BPlayer.drink(potionMeta,event.getPlayer())){ - Brew.remove(item); - item.setType(Material.POTION); - } - } + if(BPlayer.drink(Brew.getUID(item),event.getPlayer())){ + if(event.getPlayer().getGameMode() != org.bukkit.GameMode.CREATIVE){ + Brew.remove(item); } } } @@ -153,6 +142,16 @@ public class PlayerListener implements Listener{ //player walks while drunk, push him around! @EventHandler(priority = EventPriority.LOW) public void onPlayerMove(PlayerMoveEvent event){ - BPlayer.playerMove(event); + if(BPlayer.players.containsKey(event.getPlayer())){ + BPlayer.playerMove(event); + } + } + + //player talks while drunk, but he cant speak very well + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerChat(AsyncPlayerChatEvent event){ + if(BPlayer.players.containsKey(event.getPlayer())){ + Words.playerChat(event); + } } } \ No newline at end of file