Added alteration of player messages

This commit is contained in:
Sn0wStorm
2013-05-01 21:47:41 +02:00
parent c8a20f51bc
commit 610fd8342a
6 changed files with 238 additions and 65 deletions
+2 -3
View File
@@ -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){
+48 -40
View File
@@ -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){
+8
View File
@@ -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
+135
View File
@@ -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> words = new ArrayList<Words>();//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));
}
}
}
}
@@ -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);
}
}
}