Skip to content

Commit

Permalink
Merge pull request #6 from OnapleRPG/delogging
Browse files Browse the repository at this point in the history
Delogging and fixing SQLite issue
  • Loading branch information
zessirb authored Jul 18, 2020
2 parents 0fb2cea + 30da397 commit a5692ef
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 161 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ gradle.properties
version.properties
classes
gradlew.bat
.classpath
.project
.settings
bin
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,32 @@ By default, you will have two configuration files that you can change to match y

### monsters.conf
Used to generate custom monsters with special attributes. You can still use vanilla monsters, but the configured one can have improved stats.
* **damage**: Damage the monster is able to deal
* **damage**: Damage points the monster is able to deal
* **hp**: Monster health points
* **knockbackResistance**: Monster resistance to knockback
* **name**: Name to identify the monster (used when issuing commands)
* **name**: Name to identify the monster (used when issuing commands. if you include whitespace, don't forget to wrap it with "" when issuing in-game commands)
* **speed**: Monster speed
* **type**: Monster base type
* **pools**: Array of loot pooltables id, from which additionnal loot can come
* **naturalSpawn**: Natural spawn possibility (*requires more extensive documentation*)
* **equipments**: Equipments that the monster will held

### spawners.conf
Used to set spawner configuration. Each spawner you create must have a monster and a type set.
* **quantityMax**: Max monster count from the spawner
* **name**: Type name (used when issuing commands)
* **quantityMax**: Max simultaneous monster count who can come from that spawner
* **name**: Spawner type name (used when issuing commands)
* **maxSpawnRange**: Max range the monster can spawn around the spawner
* **maxRoamRange**: Max range the monster can roam around the spawner without despawning (0 for unlimited)
* **rate**: Minimum seconds before the next spawn is allowed

### loots.conf
Loot tables to add additionnal loot for custom monsters.
* **probability** Probability that the loot table will be triggered, between 0 and 1. If not triggered, none of the loots below can happen
* **name** Identifier for the loot table
* **loot** Array with the possible loots
* **weight** Probability within the loot table that the given item can drop.
It goes from the first element to the last by increasing the current weight. Exemple:
If we roll a 0.7 and the first weight is 0.5, it is skipped. If the second weight is 0.3, then our roll is matched (0.7 < 0.5 + 0.3) and the second item drops. Therefore items exceeding a total of 1 will never drop.
* **type** If you want a standard minecraft item to drop. Specify a string like "minecraft:cobblestone"
* **ref** If Itemizer is present on the server, will give an item represented within Itemizer by that ref id
* **poolId** If Itemizer is present on the server, will trigger an Itemizer pool and match its result
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ targetCompatibility = 1.8
versioning {
// required (number)
major = 1
minor = 1
patch = 1
minor = 2
patch = 0

}

version= versioning.name+'-'+versioning.build
version= versioning.name


repositories {
Expand Down Expand Up @@ -65,9 +65,9 @@ bintray {
licenses = ['Apache-2.0']
vcsUrl = 'git@github.com:OnapleRPG/Brawlator.git'
version {
name = '1.1.1'
name = '1.2.0'
released = new Date()
vcsTag = '1.1.1'
vcsTag = '1.2.0'
}
}
}
Expand All @@ -77,7 +77,7 @@ publishing {
from components.java
groupId 'com.onaple'
artifactId 'brawlator'
version '1.1.1'
version '1.2.0'
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/onaple/brawlator/Brawlator.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public void onKeyRegistration(GameRegistryEvent.Register<Key<?>> event){

@Listener
public void onDataRegistration(GameRegistryEvent.Register<DataRegistration<?, ?>> event) {
this.logger.info("onDataRegistration");
DataRegistration.builder()
.name("Monster loot")
.id("monster.loot")
Expand Down Expand Up @@ -242,15 +241,15 @@ public void onServerStart(GameStartedServerEvent event) {
initDefaultConfig("global.conf");
Brawlator.globalConfig = getConfigurationHandler().loadGlobalConfig(configDir + "/brawlator/global.conf");

loadLoot();
getLogger().info("{} monsters loaded.",loadMonsters());
getLogger().info("{} spawners types loaded.", loadSpawnerTypes());
getLogger().info("{} loot tables loaded from configuration.", loadLoot());
getLogger().info("{} monsters loaded from configuration.", loadMonsters());
getLogger().info("{} spawners types loaded from configuration.", loadSpawnerTypes());

if (Brawlator.getGlobalConfig().isEnableNaturalSpawning()) {
getLogger().info("Enable natural spawning of configured monsters.");
getLogger().info("Enabled natural spawning of configured monsters.");
Sponge.getEventManager().registerListeners(this, new NaturalSpawnListener(monsterAction,probabilityFetcher));
} else {
getLogger().info("Natural spawning disabled, to enable it edit global.conf > enableNaturalSpawning");
getLogger().info("Natural spawning of configured monsters disabled. To enable it edit global.conf > enableNaturalSpawning");
}
spawnerAction.updateSpawners();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public class MonsterDiedListener implements EventListener<BrawlatorEntityDiedEve
@Override
public void handle(BrawlatorEntityDiedEvent event) throws Exception {

Brawlator.getLogger().info("event handled {}, {}",event.getEntity().get(BrawlatorKeys.EXPERIENCE), event.getEntity().get(BrawlatorKeys.LOOT));
// Brawlator.getLogger().info("event handled {}, {}",event.getEntity().get(BrawlatorKeys.EXPERIENCE), event.getEntity().get(BrawlatorKeys.LOOT));
}
}
47 changes: 20 additions & 27 deletions src/main/java/com/onaple/brawlator/actions/MonsterAction.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.onaple.brawlator.actions;

import com.google.inject.internal.cglib.core.$AbstractClassGenerator;
import com.onaple.brawlator.Brawlator;
import com.onaple.brawlator.BrawlatorKeys;
import com.onaple.brawlator.data.beans.EquipmentBean;
import com.onaple.brawlator.data.beans.NaturalSpawnData;
import com.onaple.brawlator.data.manipulators.MonsterAdditionalModifiers;
import com.onaple.brawlator.data.manipulators.MonsterExperienceAmountManipulator;
import com.onaple.brawlator.data.manipulators.MonsterLootManipulator;
import com.onaple.brawlator.data.beans.loot.Loot;
import com.onaple.brawlator.data.beans.table.LootTable;
Expand All @@ -27,8 +21,9 @@

import javax.inject.Inject;
import javax.inject.Singleton;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
Expand Down Expand Up @@ -110,32 +105,30 @@ private Predicate<MonsterBean> isTypeEquals(EntityType entityType){
return m -> m.getType().equals(entityType);
}
private Predicate<MonsterBean> hasSpawnedInBiome(BiomeType biome){
return m -> {
if(Objects.nonNull(m.getNaturalSpawn().getBiomeType())) {
return m.getNaturalSpawn().getBiomeType().equals(biome);
}
return true;
};
return m -> {
if(Objects.nonNull(m.getNaturalSpawn().getBiomeType())) {
return m.getNaturalSpawn().getBiomeType().equals(biome);
}
return true;
};
}
private Predicate<MonsterBean> hasSpawnedBelow(int currentHeight){
return m -> {
if (m.getNaturalSpawn().getMaxHeight() > 0) {
return m.getNaturalSpawn().getMaxHeight() >= currentHeight;
}
return true;
};
return m -> {
if (m.getNaturalSpawn().getMaxHeight() > 0) {
return m.getNaturalSpawn().getMaxHeight() >= currentHeight;
}
return true;
};
}

private Entity applyLoot(Entity entity, MonsterBean monster) {
Optional<LootTable> tableOptional = probabilityFetcher.fetcher(monster.getLootTable());
Brawlator.getLogger().info("monster loot table {}",tableOptional);
if (tableOptional.isPresent()) {
List<Loot> loots = tableOptional.get().fetchLoots(random.nextDouble());
Brawlator.getLogger().info("monster loot = [{}]",loots);
MonsterLootManipulator monsterLootManipulator = entity.getOrCreate(MonsterLootManipulator.class).get();
entity.offer(monsterLootManipulator);
entity.offer(BrawlatorKeys.LOOT, loots);
List<Loot> loots = new ArrayList<>();
for (LootTable lootTable: monster.getLootTable()) {
loots.addAll(lootTable.fetchLoots(random.nextDouble()));
}
MonsterLootManipulator monsterLootManipulator = entity.getOrCreate(MonsterLootManipulator.class).get();
entity.offer(monsterLootManipulator);
entity.offer(BrawlatorKeys.LOOT, loots);
return entity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public ItemTypeLoot(double weigth, ItemType type) {

@Override
public ItemStack fetch() {
Brawlator.getLogger().info("Item Type = {}", type);
return ItemStack.builder().itemType(type).build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.onaple.brawlator.data.beans.loot;

import com.onaple.brawlator.Brawlator;
import com.onaple.itemizer.exception.ItemNotPresentException;
import com.onaple.itemizer.service.IItemService;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataQuery;
Expand All @@ -28,7 +25,6 @@ public ItemStack fetch() {
if (optionalIItemService.isPresent()) {
IItemService iItemService = optionalIItemService.get();
return iItemService.fetch(poolId).orElse(ItemStack.empty());

} else {
Brawlator.getLogger().error("Itemizer plugin not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.onaple.brawlator.Brawlator;
import com.onaple.itemizer.exception.ItemNotPresentException;
import com.onaple.itemizer.service.IItemService;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataContainer;
import org.spongepowered.api.data.DataQuery;
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/onaple/brawlator/data/beans/table/LootTable.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.onaple.brawlator.data.beans.table;

import com.onaple.brawlator.Brawlator;
import com.onaple.brawlator.data.beans.loot.Loot;
import com.onaple.brawlator.probability.Probable;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@ConfigSerializable
public class LootTable implements Probable {
Expand Down Expand Up @@ -54,12 +51,21 @@ public double getProbability() {

public List<Loot> fetchLoots(double probability){
List<Loot> allLoot = new ArrayList<>();
float cumuledWeight = 0;
List<Loot> returnedLoot = new ArrayList<>();
allLoot.addAll(getLoot());
// Inheritance system is broken (regression)
// Keeping it for now to avoid more breaking changes for users
allLoot.addAll(inherits.stream().map(LootTable::getLoot)
.flatMap(loots -> loots.stream()).collect(Collectors.toList()));
return allLoot.stream()
.filter(loot1 -> loot1.getWeight() > probability)
.collect(Collectors.toList());
for(Loot loot: allLoot) {
cumuledWeight += loot.getWeight();
if (probability <= cumuledWeight) {
returnedLoot.add(loot);
return returnedLoot;
}
}
return returnedLoot;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MonsterSpawnedDao {
* Generate database tables if they do not exist
*/
public static void createTableIfNotExist() {
String query = "CREATE TABLE IF NOT EXISTS monsterSpawned (id INTEGER PRIMARY KEY, spawnerId INTEGER, uuid VARCHAR(50), worldName VARCHAR(50))";
String query = "CREATE TABLE IF NOT EXISTS monsterSpawned (spawnerId INTEGER, uuid VARCHAR(50), worldName VARCHAR(50))";
Connection connection = null;
PreparedStatement statement = null;
try {
Expand Down Expand Up @@ -84,7 +84,7 @@ public static void addMonsterSpawned(MonsterSpawnedBean monsterSpawned) {
}

public static List<MonsterSpawnedBean> getMonstersSpawned() {
String query = "SELECT id, spawnerId, uuid, worldName FROM monsterSpawned";
String query = "SELECT spawnerId, uuid, worldName FROM monsterSpawned";
List<MonsterSpawnedBean> monsters = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
Expand All @@ -94,9 +94,8 @@ public static List<MonsterSpawnedBean> getMonstersSpawned() {
statement = connection.prepareStatement(query);
results = statement.executeQuery();
while (results.next()) {
monsters.add(new MonsterSpawnedBean(results.getInt("id"),
results.getInt("spawnerId"), UUID.fromString(results.getString("uuid")),
results.getString("worldName")));
monsters.add(new MonsterSpawnedBean(results.getInt("spawnerId"),
UUID.fromString(results.getString("uuid")), results.getString("worldName")));
}
statement.close();
} catch (ServiceUnavailableException e) {
Expand All @@ -110,7 +109,7 @@ public static List<MonsterSpawnedBean> getMonstersSpawned() {
}

public static List<MonsterSpawnedBean> getMonstersBySpawner(int spawnerId) {
String query = "SELECT id, spawnerId, uuid, worldName FROM monsterSpawned WHERE spawnerId = ?";
String query = "SELECT spawnerId, uuid, worldName FROM monsterSpawned WHERE spawnerId = ?";
List<MonsterSpawnedBean> monsters = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
Expand All @@ -121,9 +120,8 @@ public static List<MonsterSpawnedBean> getMonstersBySpawner(int spawnerId) {
statement.setInt(1, spawnerId);
results = statement.executeQuery();
while (results.next()) {
monsters.add(new MonsterSpawnedBean(results.getInt("id"),
results.getInt("spawnerId"), UUID.fromString(results.getString("uuid")),
results.getString("worldName")));
monsters.add(new MonsterSpawnedBean(results.getInt("spawnerId"),
UUID.fromString(results.getString("uuid")), results.getString("worldName")));
}
statement.close();
} catch (ServiceUnavailableException e) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/onaple/brawlator/data/dao/SpawnerDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SpawnerDao() {
* Generate database tables if they do not exist
*/
public void createTableIfNotExist() {
String query = "CREATE TABLE IF NOT EXISTS spawner (id INTEGER PRIMARY KEY, x INT, y INT, z INT, worldName VARCHAR(50), spawnerTypeName VARCHAR(50), monsterName VARCHAR(50))";
String query = "CREATE TABLE IF NOT EXISTS spawner (x INT, y INT, z INT, worldName VARCHAR(50), spawnerTypeName VARCHAR(50), monsterName VARCHAR(50))";
Connection connection = null;
PreparedStatement statement = null;
try {
Expand All @@ -53,7 +53,7 @@ public void createTableIfNotExist() {
* @return List of spawners
*/
public List<SpawnerBean> getSpawners() {
String query = "SELECT id, x, y, z, worldName, spawnerTypeName, monsterName FROM spawner";
String query = "SELECT x, y, z, worldName, spawnerTypeName, monsterName FROM spawner";
List<SpawnerBean> spawners = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
Expand All @@ -63,8 +63,7 @@ public List<SpawnerBean> getSpawners() {
statement = connection.prepareStatement(query);
results = statement.executeQuery();
while (results.next()) {
spawners.add(spawnerBuilder.buildSpawner(results.getInt("id"),
new Vector3i(results.getInt("x"), results.getInt("y"),
spawners.add(spawnerBuilder.buildSpawner(new Vector3i(results.getInt("x"), results.getInt("y"),
results.getInt("z")), results.getString("worldName"),
results.getString("spawnerTypeName"), results.getString("monsterName")));
}
Expand All @@ -80,7 +79,7 @@ public List<SpawnerBean> getSpawners() {
}

public List<SpawnerBean> getSpawnersAround(Vector3i position) {
String query = "SELECT id, x, y, z, worldName, spawnerTypeName, monsterName FROM spawner WHERE x > ? AND x < ? AND y > ? AND y < ? AND z > ? AND z < ?";
String query = "SELECT x, y, z, worldName, spawnerTypeName, monsterName FROM spawner WHERE x > ? AND x < ? AND y > ? AND y < ? AND z > ? AND z < ?";
List<SpawnerBean> spawners = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
Expand All @@ -96,8 +95,7 @@ public List<SpawnerBean> getSpawnersAround(Vector3i position) {
statement.setInt(6, position.getZ() + 2);
results = statement.executeQuery();
while (results.next()) {
spawners.add(spawnerBuilder.buildSpawner(results.getInt("id"),
new Vector3i(results.getInt("x"), results.getInt("y"),
spawners.add(spawnerBuilder.buildSpawner(new Vector3i(results.getInt("x"), results.getInt("y"),
results.getInt("z")), results.getString("worldName"),
results.getString("spawnerTypeName"), results.getString("monsterName")));
}
Expand Down Expand Up @@ -145,14 +143,16 @@ public void addSpawner(SpawnerBean spawner) {
* @param spawners List of spawners to remove
*/
public void deleteSpawners(List<SpawnerBean> spawners) {
String query = "DELETE FROM spawner WHERE id = ?";
String query = "DELETE FROM spawner WHERE x = ? AND y = ? AND z = ?";
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DatabaseHandler.getDatasource().getConnection();
for (SpawnerBean spawner : spawners) {
statement = connection.prepareStatement(query);
statement.setInt(1, spawner.getId());
statement.setInt(1, spawner.getPosition().getX());
statement.setInt(2, spawner.getPosition().getY());
statement.setInt(3, spawner.getPosition().getZ());
statement.execute();
statement.close();
}
Expand Down
Loading

0 comments on commit a5692ef

Please sign in to comment.