Implemented Player saving, more Words

This commit is contained in:
Sn0wStorm 2013-05-02 10:06:07 +02:00
parent 610fd8342a
commit 3bae71872e
5 changed files with 326 additions and 111 deletions

View File

@ -1,21 +1,21 @@
# name: versch. Namen für schlecht/mittel/gut
# ingredients: material/anzahl
# cookingtime: Zeit in echtminuten die die zutaten kochen müssen
# distillruns: wie oft destilliert werden muss 0-10 (0=ohne Destillieren)
# ingredients: material/Anzahl
# cookingtime: Zeit in Echtminuten die die Zutaten kochen müssen
# distillruns: Wie oft destilliert werden muss 0-10 (0=ohne Destillieren)
# wood: Holz des Fasses 0=alle Holzsorten 1=Birch 2=Oak 3=Jungle 4=Pine
# age: Zeit in Minecraft Tagen, die das getränk im fass reifen muss 0=kein reifen
# age: Zeit in Minecraft-Tagen, die das Getränk im Fass reifen muss 0= kein reifen
# color: Farbe des Getränks nach destillieren/reifen
# difficulty: 1-10 genauigkeit der Einhaltung der Vorgaben
# difficulty: 1-10 Genauigkeit der Einhaltung der Vorgaben (1 = ungenau/einfach 10 = sehr genau/schwer)
# alcohol: Alkoholgehalt 0-100 in Vol.% bei perfektem Getränk
#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
#words: Wörter und Buchstaben die bei Trunkenheit ersetzt werden sollen (besondere: -start = an den anfang, -end = ans ende, -random = zufällige position, -space anstatt leerzeichen)
#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
#alcohol: 1-100 trunkenheit ab der die Wörter ersetzt werden
#percentage: Wahrscheinlichkeit des Ersetzen eines Wortes in Prozent
recipes:
@ -87,6 +87,24 @@ recipes:
difficulty: 6
alcohol: 60
7:
name: Abgeranzter Vodka/Vodka/Russischer Vodka
ingredients:
- POTATO_ITEM/10
cookingtime: 15
distillruns: 3
age: 0
color: BRIGHT_GREY
difficulty: 4
alcohol: 40
8:
name: minderwertiger Absinth/Absinth/Starker Absinth
ingredients:
- LONG_GRASS/15
cookingtime: 3
distillruns: 6
color: GREEN
difficulty: 8
9:
name: Kartoffelsuppe
ingredients:
- POTATO_ITEM/5
@ -101,19 +119,127 @@ cooked:
POTATO_ITEM: Kartoffelmaische
LONG_GRASS: Kräuterbrühe
words:
ch:
- replace: ch
to: sch
pre: u,s,o,a
match: false
alcohol: 20
alcohol: 10
percentage: 70
h:
- replace: h
to: hh
pre: sch,h,t
match: false
percentage: 60
u:
alcohol: 20
- replace: u
to: uuh
percentage: 20
das:
- replace: das
to: dass
percentage: 10
alcohol: 40
alcohol: 40
- replace: '!'
to: '!!'
pre: '!'
match: false
percentage: 90
- replace: '!!'
to: '!!!111!!einself!1!'
pre: '!'
match: false
percentage: 20
alcohol: 70
- replace: betrunken
to: brhetriunkhn
pre: bist,seid
match: false
percentage: 70
alcohol: 65
- replace: laufen
to: lnhfeeehn
pre: kannst,kannst noch,kannst nicht
match: false
percentage: 80
alcohol: 85
- replace: gehen
to: hgheehn
pre: kannst,kannst noch,kannst nicht
match: false
percentage: 80
alcohol: 85
- replace: -space
to: ''
pre: h,g,w
match: true
alcohol: 10
- replace: -space
to: ''
percentage: 50
alcohol: 35
- replace: -space
to: ''
percentage: 10
- replace: -start
to: dssho
percentage: 20
alcohol: 50
- replace: -start
to: hhng
percentage: 30
alcohol: 50
- replace: -random
to: lrg
percentage: 10
- replace: -random
to: lu
percentage: 60
alcohol: 40
- replace: -random
to: blub
percentage: 60
alcohol: 70
- replace: -random
to: lerg
percentage: 40
alcohol: 75
- replace: -random
to: grulg
percentage: 50
alcohol: 80
- replace: -random
to: ''
percentage: 30
- replace: -random
to: ''
percentage: 50
alcohol: 70
- replace: -end
to: '!'
percentage: 30
alcohol: 30
- replace: -end
to: ' *hicks*'
percentage: 70
alcohol: 30

View File

@ -7,11 +7,12 @@ import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.entity.Player;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;
import org.bukkit.configuration.ConfigurationSection;
import com.dre.brewery.Brew;
public class BPlayer {
public static Map<Player,BPlayer> players=new HashMap<Player,BPlayer>();
public static Map<String,BPlayer> players=new HashMap<String,BPlayer>();//Players name and BPlayer
private int quality = 0;// = quality of drunkeness * drunkeness
private int drunkeness = 0;// = amount of drunkeness
@ -21,32 +22,35 @@ public class BPlayer {
public BPlayer(){
}
//reading from file
public BPlayer(String name,int quality,int drunkeness){
this.quality = quality;
this.drunkeness = drunkeness;
players.put(name,this);
}
public static BPlayer get(Player player){
public static BPlayer get(String name){
if(!players.isEmpty()){
if(players.containsKey(player)){
return players.get(player);
if(players.containsKey(name)){
return players.get(name);
}
}
return null;
}
/*public void remove(BPlayer player){
players.remove(player);
}*/
//returns true if drinking was successful
public static boolean drink(int uid,Player player){
public static boolean drink(int uid,String name){
Brew brew = Brew.get(uid);
if(brew != null){
BPlayer bPlayer = get(player);
BPlayer bPlayer = get(name);
if(bPlayer == null){
bPlayer = new BPlayer();
players.put(player,bPlayer);
players.put(name,bPlayer);
}
bPlayer.drunkeness += brew.getAlcohol();
bPlayer.quality += brew.getQuality() * brew.getAlcohol();
P.p.msg(player,"Du bist nun "+bPlayer.drunkeness+"% betrunken, mit einer Qualität von "+bPlayer.getQuality());
P.p.log(name+" ist nun "+bPlayer.drunkeness+"% betrunken, mit einer Qualität von "+bPlayer.getQuality());
return true;
}
return false;
@ -54,7 +58,7 @@ public class BPlayer {
//push the player around if he moves
public static void playerMove(PlayerMoveEvent event){
BPlayer bPlayer = get(event.getPlayer());
BPlayer bPlayer = get(event.getPlayer().getName());
if(bPlayer != null){
bPlayer.move(event);
}
@ -94,6 +98,29 @@ public class BPlayer {
}
}
//decreasing drunkeness over time
public static void onUpdate(){
if(!players.isEmpty()){
for(BPlayer bplayer:players.values()){
bplayer.drunkeness -= 2;
if(bplayer.drunkeness <= 0){
players.remove(bplayer);
}
}
}
}
//save all data
public static void save(ConfigurationSection config){
if(!players.isEmpty()){
for(String name:players.keySet()){
ConfigurationSection section = config.createSection(name);
section.set("quality", players.get(name).quality);
section.set("drunk", players.get(name).drunkeness);
}
}
}
//getter
public int getDrunkeness(){

View File

@ -105,13 +105,7 @@ 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;
}
}
Words.config = config;
}
//load all Data
@ -186,6 +180,15 @@ public class P extends JavaPlugin{
}
}
}
//loading BPlayer
section = data.getConfigurationSection("Player");
if(section != null){
//keys have players name
for(String name:section.getKeys(false)) {
new BPlayer(name, section.getInt(name+".quality"), section.getInt(name+".drunk"));
}
}
} else {
errorLog("No data.yml found, will create new one!");
@ -234,7 +237,10 @@ public class P extends JavaPlugin{
if(!Barrel.barrels.isEmpty()){
Barrel.save(configFile.createSection("Barrel"));
}
//BPlayer is not yet saved, as it is WIP
if(!BPlayer.players.isEmpty()){
BPlayer.save(configFile.createSection("Player"));
}
try {
configFile.save(datafile);
@ -261,6 +267,7 @@ public class P extends JavaPlugin{
cauldron.onUpdate();//runs every min to update cooking time
}
Barrel.onUpdate();//runs every min to check and update ageing time
BPlayer.onUpdate();//updates players drunkeness
saveData();//save all data
}

View File

@ -1,45 +1,69 @@
package com.dre.brewery;
import java.util.ArrayList;
import java.util.Map;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import com.dre.brewery.BPlayer;
public class Words {
//represends Words and letters, that are replaced in drunk players messages
private static ArrayList<Words> words = new ArrayList<Words>();//material and amount
public static ConfigurationSection config;
public static ArrayList<Words> words = new ArrayList<Words>();
public static FileConfiguration config;
private String from;
private String to;
private String[] pre;
private Boolean match;
private int alcohol;
private int percentage;
private Boolean match = false;
private int alcohol = 1;
private int percentage = 100;
public Words(Map<?,?> part){
for(Map.Entry<?,?> wordPart : part.entrySet()){
String key = (String) wordPart.getKey();
if(wordPart.getValue() instanceof String){
if(key.equalsIgnoreCase("replace")){
this.from = (String) wordPart.getValue();
} else if (key.equalsIgnoreCase("to")){
this.to = (String) wordPart.getValue();
} else if (key.equalsIgnoreCase("pre")){
String fullPre = (String) wordPart.getValue();
this.pre = fullPre.split(",");
}
} else if (wordPart.getValue() instanceof Boolean){
if(key.equalsIgnoreCase("match")){
this.match = (Boolean) wordPart.getValue();
}
} else if (wordPart.getValue() instanceof Integer){
if(key.equalsIgnoreCase("alcohol")){
this.alcohol = (Integer) wordPart.getValue();
} else if (key.equalsIgnoreCase("percentage")){
this.percentage = (Integer) wordPart.getValue();
}
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);
}
if(this.from != null && this.to != null){
words.add(this);
}
}
//Distort players words when he talks
public static void playerChat(AsyncPlayerChatEvent event){
BPlayer bPlayer = BPlayer.get(event.getPlayer());
BPlayer bPlayer = BPlayer.get(event.getPlayer().getName());
if(bPlayer != null){
if(words.isEmpty()){
//load when first drunk player talks
load();
}
if(!words.isEmpty()){
@ -57,76 +81,107 @@ public class Words {
//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);
if(from.equalsIgnoreCase("-end")){
from = words;
to = words+to;
} else if (from.equalsIgnoreCase("-start")){
from = words;
to = to+words;
}else if (from.equalsIgnoreCase("-space")){
from = " ";
}else if (from.equalsIgnoreCase("-random")){
//inserts "to" on a random position in "words"
int charIndex = (int) (Math.random() * (words.length() - 1));
if(charIndex > words.length() / 2){
from = words.substring(charIndex);
to = to+from;
} else {
from = words.substring(0,charIndex);
to = 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;
}
}
if (words.contains(from)){
//some characters (*,?) disturb split() which then throws PatternSyntaxException
try{
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;
//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(doesPreMatch(part, pre, match) && Math.random() * 100.0 <= percent){
//add replacement
newWords = newWords+to;
} else {
//add original
newWords = newWords+from;
}
} else {
isBefore = true;
index++;
}
if(isBefore && Math.random() * 100.0 <= percent){
//add replacement
newWords = newWords+to;
//add the last part to finish the sentence
part = splitted[index];
if(part.equals(" ")){
//dont add the space to the end
return newWords;
} else {
//add original
newWords = newWords+from;
return newWords + part;
}
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;
}
} catch (java.util.regex.PatternSyntaxException e) {
//e.printStackTrace();
return words;
}
}
return words;
}
//loaded when first drunken player speaks
public static boolean doesPreMatch(String part,String[] pre,boolean match){
boolean isBefore = !match;
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;
}
return isBefore;
}
//load from config file
public static void load(){
if(config != null){
for(String word:config.getKeys(false)){
new Words(config.getConfigurationSection(word));
for(Map<?,?> map:config.getMapList("words")){
new Words(map);
}
}
}

View File

@ -129,7 +129,7 @@ public class PlayerListener implements Listener{
if(item != null){
if(item.getType() == Material.POTION){
if(item.hasItemMeta()){
if(BPlayer.drink(Brew.getUID(item),event.getPlayer())){
if(BPlayer.drink(Brew.getUID(item),event.getPlayer().getName())){
if(event.getPlayer().getGameMode() != org.bukkit.GameMode.CREATIVE){
Brew.remove(item);
}
@ -142,7 +142,7 @@ public class PlayerListener implements Listener{
//player walks while drunk, push him around!
@EventHandler(priority = EventPriority.LOW)
public void onPlayerMove(PlayerMoveEvent event){
if(BPlayer.players.containsKey(event.getPlayer())){
if(BPlayer.players.containsKey(event.getPlayer().getName())){
BPlayer.playerMove(event);
}
}
@ -150,7 +150,7 @@ public class PlayerListener implements Listener{
//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())){
if(BPlayer.players.containsKey(event.getPlayer().getName())){
Words.playerChat(event);
}
}