Items can lower Drunkeness when consumed

This commit is contained in:
Sn0wStorm 2013-08-14 21:18:11 +02:00
parent e2e6448cf6
commit ac9975fce9
5 changed files with 58 additions and 13 deletions

View File

@ -19,7 +19,7 @@ homeType: 'cmd: home'
enableWake: true
# Der Text, den der Spieler beim Aufwachen sieht
wakeString: 'Ohh nein! Ich kann mich nicht erinnern, wie ich hier hergekommen bin...'
wakeString: 'Ohh nein! Ich kann mich nicht erinnern, wie ich hier hergekommen bin... *hicks*'
# Ob der Spieler sich bei großer Trunkenheit teilweise nicht einloggen kann und kurz warten muss, da sein Charakter nicht reagiert
enableLoginDisallow: true
@ -34,6 +34,11 @@ enablePuke: true
# Item das beim Erbrechen mehrfach unaufsammelbar fallen gelassen wird
pukeItem: SOUL_SAND
# Konsumierbares Item/<Stärke>. Senkt den Alkoholpegel um <Stärke>% wenn konsumiert.
drainItems:
- BREAD/4
- MILK_BUCKET/2
# Autosave Intervall in Minuten
autosave: 3

View File

@ -9,6 +9,7 @@ import org.bukkit.entity.Player;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Material;
import org.bukkit.util.Vector;
import org.bukkit.Location;
import org.bukkit.potion.PotionEffectType;
@ -18,6 +19,7 @@ import com.dre.brewery.Brew;
public class BPlayer {
public static Map<String, BPlayer> players = new HashMap<String, BPlayer>();// Players name and BPlayer
public static Map<Material, Integer> drainItems = new HashMap<Material, Integer>();// DrainItem Material and Strength
private static Map<Player, Integer> pTasks = new HashMap<Player, Integer>();// Player and count
private static int taskId;
public static int pukeItemId;
@ -121,6 +123,30 @@ public class BPlayer {
}
}
// Eat something to drain the drunkeness
public void drainByItem(String name, Material mat) {
int strength = drainItems.get(mat);
if (drain(name, strength)) {
players.remove(name);
}
}
// drain the drunkeness by amount, returns true when player has to be removed
public boolean drain(String name, int amount) {
quality -= getQuality() * amount;
drunkeness -= amount;
if (drunkeness > 0) {
if (offlineDrunk == 0) {
if (getPlayer(name) == null) {
offlineDrunk = drunkeness;
}
}
} else if (drunkeness <= (-1) * offlineDrunk) {
return true;
}
return false;
}
// player is drunk
public void move(PlayerMoveEvent event) {
// has player more alc than 10
@ -400,15 +426,7 @@ public class BPlayer {
// Prevent 0 drunkeness
soberPerMin++;
}
bplayer.quality -= bplayer.getQuality() * soberPerMin;
bplayer.drunkeness -= soberPerMin;
if (bplayer.drunkeness > 0) {
if (bplayer.offlineDrunk == 0) {
if (getPlayer(name) == null) {
bplayer.offlineDrunk = bplayer.drunkeness;
}
}
} else if (bplayer.drunkeness <= (-1) * bplayer.offlineDrunk) {
if (bplayer.drain(name, soberPerMin)) {
iter.remove();
}
}

View File

@ -46,7 +46,6 @@ public class BRecipe {
if (effectStringList != null) {
for (String effectString : effectStringList) {
String[] effectSplit = effectString.split("/");
P.p.log("effekt: " + effectSplit[0]);
if (effectSplit.length > 1) {
effects.put(effectSplit[0], P.p.parseInt(effectSplit[1]));
} else {

View File

@ -1,6 +1,7 @@
package com.dre.brewery;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.HashMap;
@ -101,6 +102,7 @@ public class P extends JavaPlugin {
BIngredients.recipes.clear();
BIngredients.cookedNames.clear();
Words.words.clear();
BPlayer.drainItems.clear();
readConfig();
}
@ -154,6 +156,21 @@ public class P extends JavaPlugin {
}
}
// loading drainItems
List<String> drainList = config.getStringList("drainItems");
if (drainList != null) {
for (String drainString : drainList) {
String[] drainSplit = drainString.split("/");
if (drainSplit.length > 1) {
Material mat = Material.matchMaterial(drainSplit[0]);
int strength = p.parseInt(drainSplit[1]);
if (mat != null && strength > 0) {
BPlayer.drainItems.put(mat, strength);
}
}
}
}
// telling Words the path, it will load it when needed
Words.config = config;
}

View File

@ -107,16 +107,22 @@ public class PlayerListener implements Listener {
// player drinks a custom potion
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
Player player = event.getPlayer();
ItemStack item = event.getItem();
if (item != null) {
if (item.getType() == Material.POTION) {
if (item.hasItemMeta()) {
if (BPlayer.drink(Brew.getUID(item), event.getPlayer())) {
if (event.getPlayer().getGameMode() != org.bukkit.GameMode.CREATIVE) {
if (BPlayer.drink(Brew.getUID(item), player)) {
if (player.getGameMode() != org.bukkit.GameMode.CREATIVE) {
Brew.remove(item);
}
}
}
} else if (BPlayer.drainItems.containsKey(item.getType())) {
BPlayer bplayer = BPlayer.get(player.getName());
if (bplayer != null) {
bplayer.drainByItem(player.getName(), item.getType());
}
}
}
}