317 lines
9.3 KiB
Java

package com.bernard.hgWorld.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.Event;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import com.bernard.hgWorld.math.UndeterminedInt;
import com.bernard.hgWorld.math.UndeterminedString;
import com.bernard.inutil.ArrayInutil;
public class HgConfig {
//Scenarios
public Map<String,ScenarEvent> events;
public static class ScenarEvent{
public String condition;
public boolean persistent;
public int delay;
public int loop;
public List<DelayedMessage> warnHappen; //Est affiché lorsque l'action est déclenchée
public List<DelayedMessage> warnFailed; //Est affiché lorsque (loop ou after) s'est déclenché mais que la condition a empeche le déroulement
public List<DelayedMessage> warnCondition; //Est affiché lorsque (loop ou after ou condition) est déclenché, ne reste que le délai
public double spawn;
public Action[] actions;
public class DelayedMessage{
public String message;
public int delay;
}
public static class Action{
Event initializer;
String type;
Param[] params;
public void execute(Event initializer) {
//TODO l'execution d'actions avec initialiseur
/// /!\ Effectue juste les tests ceux de l'event !
}
public static class Param{
String name;
String value;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((initializer == null) ? 0 : initializer.hashCode());
result = prime * result + Arrays.hashCode(params);
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Action other = (Action) obj;
if (initializer == null) {
if (other.initializer != null)
return false;
} else if (!initializer.equals(other.initializer))
return false;
if (!Arrays.equals(params, other.params))
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(actions);
result = prime * result + ((condition == null) ? 0 : condition.hashCode());
result = prime * result + delay;
result = prime * result + loop;
result = prime * result + (persistent ? 1231 : 1237);
long temp;
temp = Double.doubleToLongBits(spawn);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((warnCondition == null) ? 0 : warnCondition.hashCode());
result = prime * result + ((warnFailed == null) ? 0 : warnFailed.hashCode());
result = prime * result + ((warnHappen == null) ? 0 : warnHappen.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ScenarEvent other = (ScenarEvent) obj;
if (!Arrays.equals(actions, other.actions))
return false;
if (condition == null) {
if (other.condition != null)
return false;
} else if (!condition.equals(other.condition))
return false;
if (delay != other.delay)
return false;
if (loop != other.loop)
return false;
if (persistent != other.persistent)
return false;
if (Double.doubleToLongBits(spawn) != Double.doubleToLongBits(other.spawn))
return false;
if (warnCondition == null) {
if (other.warnCondition != null)
return false;
} else if (!warnCondition.equals(other.warnCondition))
return false;
if (warnFailed == null) {
if (other.warnFailed != null)
return false;
} else if (!warnFailed.equals(other.warnFailed))
return false;
if (warnHappen == null) {
if (other.warnHappen != null)
return false;
} else if (!warnHappen.equals(other.warnHappen))
return false;
return true;
}
}
//ChestTypes
public Map<String,ChestType> chestTypes = new HashMap<>();
public static class ChestType{//TODO Optimize with a cool bernard API
Map<ItemGroup,Double> groups;
Map<UndeterminedItem,Double> items; //Item->Chances de spawn
public Inventory fillInventory(Inventory inv) {
inv.clear();
Random rand = new Random();
List<UndeterminedItem> toSpawn = new ArrayList<>();
for (Entry<ItemGroup, Double> itemGroup : groups.entrySet())
if(rand.nextDouble() <= itemGroup.getValue())
toSpawn.addAll(itemGroup.getKey().getItems());
for (Entry<UndeterminedItem, Double> item : items.entrySet())
if(rand.nextDouble() <= item.getValue())
toSpawn.add(item.getKey());
if(inv.getSize() < toSpawn.size()) {
Collections.shuffle(toSpawn);
for(int i = toSpawn.size()-1;i>inv.getSize()-1;i--)
toSpawn.remove(i);
}
ItemStack[] content = new ItemStack[inv.getSize()];
for (UndeterminedItem item : toSpawn) {
int slot = item.slot;
if(slot == -1)
slot = rand.nextInt(content.length);
else if(content[item.slot] != null){//Echanger les items (le nouveau prends la place de l'ancien, qui est replacé aléatoirement, car considéré comme un nouveau)
UndeterminedItem nouveau = item;
item = new UndeterminedItem(content[slot]);
content[slot] = nouveau.getItem();
}
while(content[slot] != null)
slot = (slot+1)%content.length;
content[slot] = item.getItem();
}
inv.setContents(content);
return inv;
}
public static class ItemGroup{
String name;
UndeterminedInt count;
Map<UndeterminedItem,Double> items; //Item->spawnWeight
public List<UndeterminedItem> getItems() {
Random rand = new Random();
int count = this.count.generateInt();
double[] spawnWeights = new double[items.size()];
double totalWeigth = 0;
List<UndeterminedItem> it = new ArrayList<>(items.keySet());
for (int i = 0;i<spawnWeights.length;i++) {
spawnWeights[i] = items.get(it.get(i));
totalWeigth += spawnWeights[i];
}
return ArrayInutil.asize(it, count, ArrayInutil.toObjectArray(spawnWeights),totalWeigth, rand);
}
}
public static class UndeterminedItem{
UndeterminedInt stackSize;
UndeterminedInt damage;
Material type;
int slot;
Map<EnchantementGroup,Double> enchantGroups = new HashMap<>();
Map<UndeterminedEnchantement,Double> enchants = new HashMap<>(); //Item->Chances de spawn
UndeterminedString name;
public UndeterminedItem(ItemStack s) {
this.slot = -1;
this.stackSize = new UndeterminedInt(s.getAmount());
this.type = s.getType();
this.damage = new UndeterminedInt(s.getDurability());
this.name = s.getItemMeta().hasDisplayName()?new UndeterminedString(s.getItemMeta().getDisplayName()):null;
}
public ItemStack getItem(){
ItemStack stack = new ItemStack(type, stackSize.generateInt(), (short) damage.generateInt());
Random rand = new Random();
List<UndeterminedEnchantement> toSpawn = new ArrayList<>();
for (Entry<EnchantementGroup, Double> itemGroup : enchantGroups.entrySet())
if(rand.nextDouble() <= itemGroup.getValue())
toSpawn.addAll(itemGroup.getKey().getEnchantements());
for (Entry<UndeterminedEnchantement, Double> item : enchants.entrySet())
if(rand.nextDouble() <= item.getValue())
toSpawn.add(item.getKey());
for (UndeterminedEnchantement undeterminedEnchantement : toSpawn)
stack.addUnsafeEnchantment(undeterminedEnchantement.ID, undeterminedEnchantement.level.generateInt());
if(name != null)
stack.getItemMeta().setDisplayName(name.generate());//TODO verifier le fonctionnement
return stack;
}
public static class EnchantementGroup{
String name;
UndeterminedInt count;
Map<UndeterminedEnchantement,Double> items; //Enchant->spawnWeight
public List<UndeterminedEnchantement> getEnchantements() {
Random rand = new Random();
int count = this.count.generateInt();
double[] spawnWeights = new double[items.size()];
double totalWeigth = 0;
List<UndeterminedEnchantement> it = new ArrayList<>(items.keySet());
for (int i = 0;i<spawnWeights.length;i++) {
spawnWeights[i] = items.get(it.get(i));
totalWeigth += spawnWeights[i];
}
return ArrayInutil.asize(it, count, ArrayInutil.toObjectArray(spawnWeights),totalWeigth, rand);
}
}
public static class UndeterminedEnchantement{
Enchantment ID;
UndeterminedInt level;
public Enchantment getEnchantement() {
return ID;
}
public short getLevel() {
return (short) level.generateInt();
}
}
}
}
}