mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 06:38:56 +00:00
Added alteration of player messages
This commit is contained in:
+24
@@ -10,6 +10,13 @@
|
|||||||
|
|
||||||
#cooked: Auflistung ALLER möglichen Zutaten und die daraus entstehenden Tranknamen: (leer für undef.)
|
#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:
|
recipes:
|
||||||
1:
|
1:
|
||||||
@@ -93,3 +100,20 @@ cooked:
|
|||||||
APPLE: Apfelmost
|
APPLE: Apfelmost
|
||||||
POTATO_ITEM: Kartoffelmaische
|
POTATO_ITEM: Kartoffelmaische
|
||||||
LONG_GRASS: Kräuterbrühe
|
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
|
||||||
@@ -6,7 +6,6 @@ import java.util.HashMap;
|
|||||||
import org.bukkit.event.player.PlayerMoveEvent;
|
import org.bukkit.event.player.PlayerMoveEvent;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.inventory.meta.PotionMeta;
|
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
|
||||||
import com.dre.brewery.Brew;
|
import com.dre.brewery.Brew;
|
||||||
@@ -37,8 +36,8 @@ public class BPlayer {
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
//returns true if drinking was successful
|
//returns true if drinking was successful
|
||||||
public static boolean drink(PotionMeta meta,Player player){
|
public static boolean drink(int uid,Player player){
|
||||||
Brew brew = Brew.get(meta);
|
Brew brew = Brew.get(uid);
|
||||||
if(brew != null){
|
if(brew != null){
|
||||||
BPlayer bPlayer = get(player);
|
BPlayer bPlayer = get(player);
|
||||||
if(bPlayer == null){
|
if(bPlayer == null){
|
||||||
|
|||||||
@@ -48,13 +48,56 @@ public class Brew {
|
|||||||
potions.put(uid,this);
|
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!)
|
//remove potion from map (drinking, despawning, should be more!)
|
||||||
public static void remove(ItemStack item){
|
public static void remove(ItemStack item){
|
||||||
PotionMeta meta = (PotionMeta) item.getItemMeta();
|
potions.remove(getUID(item));
|
||||||
Brew brew = get(meta);
|
|
||||||
if(brew != null){
|
|
||||||
potions.remove(brew);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -67,41 +110,6 @@ public class Brew {
|
|||||||
return uid;
|
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
|
//calculate alcohol from recipe
|
||||||
public int calcAlcohol(int alc){
|
public int calcAlcohol(int alc){
|
||||||
if(distillRuns == 0){
|
if(distillRuns == 0){
|
||||||
|
|||||||
@@ -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
|
//load all Data
|
||||||
|
|||||||
@@ -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.PlayerInteractEvent;
|
||||||
import org.bukkit.event.player.PlayerItemConsumeEvent;
|
import org.bukkit.event.player.PlayerItemConsumeEvent;
|
||||||
import org.bukkit.event.player.PlayerMoveEvent;
|
import org.bukkit.event.player.PlayerMoveEvent;
|
||||||
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||||
import org.bukkit.event.inventory.BrewEvent;
|
import org.bukkit.event.inventory.BrewEvent;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
@@ -15,15 +16,12 @@ import org.bukkit.inventory.BrewerInventory;
|
|||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
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.BCauldron;
|
||||||
import com.dre.brewery.BIngredients;
|
import com.dre.brewery.BIngredients;
|
||||||
import com.dre.brewery.Brew;
|
import com.dre.brewery.Brew;
|
||||||
import com.dre.brewery.Barrel;
|
import com.dre.brewery.Barrel;
|
||||||
import com.dre.brewery.BPlayer;
|
import com.dre.brewery.BPlayer;
|
||||||
|
import com.dre.brewery.Words;
|
||||||
|
|
||||||
public class PlayerListener implements Listener{
|
public class PlayerListener implements Listener{
|
||||||
@EventHandler(priority = EventPriority.HIGH)
|
@EventHandler(priority = EventPriority.HIGH)
|
||||||
@@ -107,13 +105,10 @@ public class PlayerListener implements Listener{
|
|||||||
if(item != null){
|
if(item != null){
|
||||||
if(item.getType() == Material.POTION){
|
if(item.getType() == Material.POTION){
|
||||||
if(item.hasItemMeta()){
|
if(item.hasItemMeta()){
|
||||||
PotionMeta potionMeta = ((PotionMeta) item.getItemMeta());
|
if(Brew.potions.containsKey(Brew.getUID(item))){
|
||||||
if(potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)){
|
//has custom potion in "slot"
|
||||||
if(Brew.get(potionMeta) != null){
|
contents[slot] = 1;
|
||||||
//has custom potion in "slot"
|
custom = true;
|
||||||
contents[slot] = 1;
|
|
||||||
custom = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,15 +129,9 @@ public class PlayerListener implements Listener{
|
|||||||
if(item != null){
|
if(item != null){
|
||||||
if(item.getType() == Material.POTION){
|
if(item.getType() == Material.POTION){
|
||||||
if(item.hasItemMeta()){
|
if(item.hasItemMeta()){
|
||||||
PotionMeta potionMeta = ((PotionMeta) item.getItemMeta());
|
if(BPlayer.drink(Brew.getUID(item),event.getPlayer())){
|
||||||
if(potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)){
|
if(event.getPlayer().getGameMode() != org.bukkit.GameMode.CREATIVE){
|
||||||
for(PotionEffect effect:potionMeta.getCustomEffects()){
|
Brew.remove(item);
|
||||||
if(effect.getType().getId() == 10){
|
|
||||||
if(BPlayer.drink(potionMeta,event.getPlayer())){
|
|
||||||
Brew.remove(item);
|
|
||||||
item.setType(Material.POTION);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,6 +142,16 @@ public class PlayerListener implements Listener{
|
|||||||
//player walks while drunk, push him around!
|
//player walks while drunk, push him around!
|
||||||
@EventHandler(priority = EventPriority.LOW)
|
@EventHandler(priority = EventPriority.LOW)
|
||||||
public void onPlayerMove(PlayerMoveEvent event){
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user