mirror of
https://github.com/exituser/Brewery.git
synced 2026-09-17 07:49:03 +00:00
Implemented Player saving, more Words
This commit is contained in:
@@ -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(){
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
+128
-73
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user