3.0.0-SNAPSHOT-U18

Added BossesFileHandler, BossesFileManager as well as updating MainStatsElement in the bosses.json file to allow for multiple bosses (stacking) as well as updating the EntityTypeMechanic to support the new multiple bosses option. Also finished off the createBaseBossEntity method in the BossAPI.
This commit is contained in:
AMinecraftDev 2018-07-01 23:30:18 +08:00
parent 7d08f613d0
commit 1acd70265b
17 changed files with 298 additions and 62 deletions

View File

@ -1,11 +1,14 @@
{
"SkeletonKing": {
"editing": false,
"mainStats": {
"entityType": "SKELETON",
"health": 500,
"displayName": "&6&lSkeleton King Boss"
},
"editing": true,
"mainStats": [
{
"position": 1,
"entityType": "SKELETON",
"health": 500,
"displayName": "&6&lSkeleton King Boss"
}
],
"spawnItem": "SKSpawnEgg",
"equipment": {
"helmet": "SKHelmet",

View File

@ -1,8 +1,11 @@
package net.aminecraftdev.custombosses;
import lombok.Getter;
import net.aminecraftdev.custombosses.managers.BossItemFileManager;
import net.aminecraftdev.custombosses.api.BossAPI;
import net.aminecraftdev.custombosses.file.BossesFileHandler;
import net.aminecraftdev.custombosses.managers.files.BossItemFileManager;
import net.aminecraftdev.custombosses.managers.BossMechanicManager;
import net.aminecraftdev.custombosses.managers.files.BossesFileManager;
import net.aminecraftdev.custombosses.utils.IReloadable;
import org.bukkit.plugin.java.JavaPlugin;
@ -15,10 +18,14 @@ public class CustomBosses extends JavaPlugin implements IReloadable {
@Getter private BossMechanicManager bossMechanicManager;
@Getter private BossItemFileManager itemStackManager;
@Getter private BossesFileManager bossesFileManager;
@Override
public void onEnable() {
BossAPI.setPlugin(this);
this.itemStackManager = new BossItemFileManager(this);
this.bossesFileManager = new BossesFileManager(this);
this.bossMechanicManager = new BossMechanicManager(this);
reload();
@ -28,6 +35,7 @@ public class CustomBosses extends JavaPlugin implements IReloadable {
@Override
public void reload() {
this.itemStackManager.reload();
this.bossesFileManager.reload();
this.bossMechanicManager.load();
}
}

View File

@ -1,12 +1,14 @@
package net.aminecraftdev.custombosses.api;
import net.aminecraftdev.custombosses.CustomBosses;
import net.aminecraftdev.custombosses.entity.BossEntity;
import net.aminecraftdev.custombosses.entity.elements.*;
import net.aminecraftdev.custombosses.utils.EntityTypeUtil;
import net.aminecraftdev.custombosses.utils.Debug;
import net.aminecraftdev.custombosses.utils.EntityFinder;
import net.aminecraftdev.custombosses.utils.potion.holder.PotionEffectHolder;
import org.bukkit.entity.EntityType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -16,8 +18,25 @@ import java.util.List;
*/
public class BossAPI {
public static BossEntity createBoss(String name, String entityTypeInput) {
MainStatsElement mainStatsElement = new MainStatsElement();
private static CustomBosses PLUGIN;
/**
* Used to create a base BossEntity model which
* can be used to fine tune and then once the main
* elements are filled in editing can be disabled
* and the boss can be spawned.
*
* @param name - boss name
* @param entityTypeInput - entity type
* @return null if something went wrong, or the BossEntity that was created.
*/
public static BossEntity createBaseBossEntity(String name, String entityTypeInput) {
String input = entityTypeInput.split(":")[0];
EntityFinder entityFinder = EntityFinder.get(input);
if (entityFinder == null) return null;
List<MainStatsElement> mainStatsElements = new ArrayList<>(Arrays.asList(new MainStatsElement()));
EquipmentElement equipmentElement = new EquipmentElement();
HandsElement handsElement = new HandsElement();
List<PotionEffectHolder> potionEffectHolders = new ArrayList<>();
@ -26,12 +45,41 @@ public class BossAPI {
MessagesElement messagesElement = new MessagesElement();
CommandsElement commandsElement = new CommandsElement();
//TODO: Set the entityType to said entityTypeInput
BossEntity bossEntity = new BossEntity(true, null, mainStatsElements, equipmentElement, handsElement, potionEffectHolders, skillsElement, dropsElement, messagesElement, commandsElement);
MainStatsElement mainStatsElement = mainStatsElements.get(0);
BossEntity bossEntity = new BossEntity(true, null, mainStatsElement, equipmentElement, handsElement, potionEffectHolders, skillsElement, dropsElement, messagesElement, commandsElement);
mainStatsElement.setEntityType(entityFinder.getFancyName());
mainStatsElement.setDisplayName(name);
mainStatsElement.setHealth(50D);
boolean result = PLUGIN.getBossesFileManager().saveNewBossEntity(name, bossEntity);
if (!result) {
Debug.BOSS_NAME_EXISTS.debug(name);
return null;
}
return bossEntity;
}
/**
* Used to update the variable to the
* plugin instance so the methods can
* pull variables in the main class to use
* in their method.
*
* This should only ever be used in house and
* never to be used by an outside party.
*
* @param customBosses - the plugin instance.
*/
public static void setPlugin(CustomBosses customBosses) {
if(PLUGIN != null) {
Debug.ATTEMPTED_TO_UPDATE_PLUGIN.debug();
return;
}
PLUGIN = customBosses;
}
}

View File

@ -16,7 +16,7 @@ import java.util.List;
public class BossEntity {
@Expose @Getter private final List<PotionEffectHolder> potions;
@Expose @Getter private final MainStatsElement mainStats;
@Expose @Getter private final List<MainStatsElement> mainStats;
@Expose @Getter private final EquipmentElement equipment;
@Expose @Getter private final MessagesElement messages;
@Expose @Getter private final CommandsElement commands;
@ -27,7 +27,7 @@ public class BossEntity {
@Expose @Getter @Setter private String spawnItem;
@Expose @Getter @Setter private boolean editing;
public BossEntity(boolean editing, String spawnItem, MainStatsElement mainStats, EquipmentElement equipment, HandsElement hands, List<PotionEffectHolder> potions,
public BossEntity(boolean editing, String spawnItem, List<MainStatsElement> mainStats, EquipmentElement equipment, HandsElement hands, List<PotionEffectHolder> potions,
SkillsElement skills, DropsElement drops, MessagesElement messages, CommandsElement commands) {
this.editing = editing;
this.mainStats = mainStats;

View File

@ -11,6 +11,7 @@ import lombok.Setter;
*/
public class MainStatsElement {
@Expose @Getter @Setter private Integer position;
@Expose @Getter @Setter private String entityType;
@Expose @Getter @Setter private Double health;
@Expose @Getter @Setter private String displayName;

View File

@ -0,0 +1,75 @@
package net.aminecraftdev.custombosses.file;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import net.aminecraftdev.custombosses.entity.BossEntity;
import net.aminecraftdev.custombosses.utils.file.FileHandler;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
/**
* @author Charles Cullen
* @version 1.0.0
* @since 01-Jul-18
*/
public class BossesFileHandler extends FileHandler<Map<String, BossEntity>> {
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.create();
public BossesFileHandler(JavaPlugin javaPlugin, boolean saveResource, File file) {
super(javaPlugin, saveResource, file);
}
@Override
public Map<String, BossEntity> loadFile() {
Map<String, BossEntity> bossEntityMap = new HashMap<>();
createFile();
try {
FileReader fileReader = new FileReader(getFile());
JsonObject jsonObject = GSON.fromJson(fileReader, JsonObject.class);
fileReader.close();
if(jsonObject != null) {
jsonObject.entrySet().forEach(entry -> {
String id = entry.getKey();
BossEntity bossEntity = GSON.fromJson(entry.getValue(), BossEntity.class);
bossEntityMap.put(id, bossEntity);
});
}
} catch (IOException ex) {
ex.printStackTrace();
}
return bossEntityMap;
}
@Override
public void saveFile(Map<String, BossEntity> map) {
try {
FileWriter fileWriter = new FileWriter(getFile());
Type type = new TypeToken<Map<String, BossEntity>>(){}.getType();
fileWriter.write(GSON.toJson(new HashMap<>(map), type));
fileWriter.flush();
fileWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

View File

@ -1,9 +1,10 @@
package net.aminecraftdev.custombosses.utils.itemstack.handlers;
package net.aminecraftdev.custombosses.file;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import net.aminecraftdev.custombosses.utils.file.FileHandler;
import net.aminecraftdev.custombosses.utils.file.FileUtils;
import net.aminecraftdev.custombosses.utils.file.IFileHandler;
import net.aminecraftdev.custombosses.utils.itemstack.holder.ItemStackHolder;
@ -22,41 +23,25 @@ import java.util.Map;
* @version 1.0.0
* @since 28-Apr-18
*/
public class ItemStackFileHandler implements IFileHandler<Map<String, ItemStackHolder>> {
public class ItemStackFileHandler extends FileHandler<Map<String, ItemStackHolder>> {
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.create();
private final JavaPlugin javaPlugin;
private final boolean saveResource;
private final File file;
public ItemStackFileHandler(JavaPlugin javaPlugin, File file, boolean saveResource) {
this.saveResource = saveResource;
this.javaPlugin = javaPlugin;
this.file = file;
super(javaPlugin, saveResource, file);
}
@Override
public Map<String, ItemStackHolder> loadFile() {
Map<String, ItemStackHolder> itemStackHolderMap = new HashMap<>();
if(!this.file.exists()) {
if(this.saveResource) {
String path = this.file.getName();
if(this.javaPlugin.getResource(path) != null) {
this.javaPlugin.saveResource(path, false);
}
} else {
FileUtils.get().createFile(this.file);
}
}
createFile();
try {
FileReader fileReader = new FileReader(file);
FileReader fileReader = new FileReader(getFile());
JsonObject jsonObject = GSON.fromJson(fileReader, JsonObject.class);
fileReader.close();
@ -79,7 +64,7 @@ public class ItemStackFileHandler implements IFileHandler<Map<String, ItemStackH
@Override
public void saveFile(Map<String, ItemStackHolder> map) {
try {
FileWriter fileWriter = new FileWriter(file);
FileWriter fileWriter = new FileWriter(getFile());
Type type = new TypeToken<Map<String, ItemStackHolder>>(){}.getType();
fileWriter.write(GSON.toJson(new HashMap<>(map), type));

View File

@ -6,6 +6,9 @@ import net.aminecraftdev.custombosses.exception.AlreadySetException;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import java.util.HashMap;
import java.util.Map;
/**
* @author Charles Cullen
* @version 1.0.0
@ -16,18 +19,18 @@ public class ActiveBossHolder {
@Getter private final BossEntity bossEntity;
@Getter private final Location location;
@Getter private LivingEntity livingEntity;
@Getter private Map<Integer, LivingEntity> livingEntityMap = new HashMap<>();
public ActiveBossHolder(BossEntity bossEntity, Location spawnLocation) {
this.location = spawnLocation;
this.bossEntity = bossEntity;
}
public void setLivingEntity(LivingEntity livingEntity) {
if(getLivingEntity() != null) {
public void setLivingEntity(int position, LivingEntity livingEntity) {
if(getLivingEntityMap().containsKey(position)) {
throw new AlreadySetException("Tried to set a new LivingEntity while it's already set.");
} else {
this.livingEntity = livingEntity;
this.livingEntityMap.put(position, livingEntity);
}
}
}

View File

@ -47,13 +47,21 @@ public class BossMechanicManager implements ILoadable {
if(mechanic == null) continue;
if(!mechanic.applyMechanic(bossEntity, activeBossHolder)) {
Debug.MECHANIC_APPLICATION_FAILED.debug(mechanic.getClass().getSimpleName());
return false;
}
if(!handleMechanicApplication(mechanic, bossEntity, activeBossHolder)) return false;
}
}
return true;
}
private boolean handleMechanicApplication(IMechanic mechanic, BossEntity bossEntity, ActiveBossHolder activeBossHolder) {
if(mechanic == null) return false;
if(!mechanic.applyMechanic(bossEntity, activeBossHolder)) {
Debug.MECHANIC_APPLICATION_FAILED.debug(mechanic.getClass().getSimpleName());
return false;
}
return true;
}
}

View File

@ -1,11 +1,11 @@
package net.aminecraftdev.custombosses.managers;
package net.aminecraftdev.custombosses.managers.files;
import lombok.Getter;
import net.aminecraftdev.custombosses.utils.ILoadable;
import net.aminecraftdev.custombosses.utils.IReloadable;
import net.aminecraftdev.custombosses.utils.ISavable;
import net.aminecraftdev.custombosses.utils.itemstack.ItemStackConverter;
import net.aminecraftdev.custombosses.utils.itemstack.handlers.ItemStackFileHandler;
import net.aminecraftdev.custombosses.file.ItemStackFileHandler;
import net.aminecraftdev.custombosses.utils.itemstack.holder.ItemStackHolder;
import org.bukkit.plugin.java.JavaPlugin;

View File

@ -0,0 +1,58 @@
package net.aminecraftdev.custombosses.managers.files;
import lombok.Getter;
import net.aminecraftdev.custombosses.entity.BossEntity;
import net.aminecraftdev.custombosses.file.BossesFileHandler;
import net.aminecraftdev.custombosses.utils.ILoadable;
import net.aminecraftdev.custombosses.utils.IReloadable;
import net.aminecraftdev.custombosses.utils.ISavable;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* @author Charles Cullen
* @version 1.0.0
* @since 01-Jul-18
*/
public class BossesFileManager implements ILoadable, ISavable, IReloadable {
private Map<String, BossEntity> bossEntityMap = new HashMap<>();
private BossesFileHandler bossesFileHandler;
public BossesFileManager(JavaPlugin javaPlugin) {
File file = new File(javaPlugin.getDataFolder(), "bosses.json");
this.bossesFileHandler = new BossesFileHandler(javaPlugin, true, file);
}
@Override
public void load() {
this.bossEntityMap = this.bossesFileHandler.loadFile();
}
@Override
public void reload() {
load();
}
@Override
public void save() {
this.bossesFileHandler.saveFile(this.bossEntityMap);
}
public boolean saveNewBossEntity(String name, BossEntity bossEntity) {
if(this.bossEntityMap.containsKey(name)) {
return false;
}
this.bossEntityMap.put(name, bossEntity);
return true;
}
public BossEntity getBossEntity(String name) {
return this.bossEntityMap.getOrDefault(name, null);
}
}

View File

@ -1,6 +1,7 @@
package net.aminecraftdev.custombosses.mechanics;
import net.aminecraftdev.custombosses.entity.BossEntity;
import net.aminecraftdev.custombosses.entity.elements.MainStatsElement;
import net.aminecraftdev.custombosses.holder.ActiveBossHolder;
import net.aminecraftdev.custombosses.utils.EntityFinder;
import net.aminecraftdev.custombosses.utils.IMechanic;
@ -15,17 +16,22 @@ public class EntityTypeMechanic implements IMechanic {
@Override
public boolean applyMechanic(BossEntity bossEntity, ActiveBossHolder activeBossHolder) {
String bossEntityType = bossEntity.getMainStats().getEntityType();
String input = bossEntityType.split(":")[0];
EntityFinder entityFinder = EntityFinder.get(input);
for(MainStatsElement mainStatsElement : bossEntity.getMainStats()) {
String bossEntityType = mainStatsElement.getEntityType();
String input = bossEntityType.split(":")[0];
EntityFinder entityFinder = EntityFinder.get(input);
Integer position = mainStatsElement.getPosition();
if(entityFinder == null) return false;
if(position == null) position = 1;
if(entityFinder == null) return false;
LivingEntity livingEntity = entityFinder.spawnNewLivingEntity(bossEntityType, activeBossHolder.getLocation());
LivingEntity livingEntity = entityFinder.spawnNewLivingEntity(bossEntityType, activeBossHolder.getLocation());
if(livingEntity == null) return false;
if(livingEntity == null) return false;
activeBossHolder.setLivingEntity(position, livingEntity);
}
activeBossHolder.setLivingEntity(livingEntity);
return true;
}
}

View File

@ -3,7 +3,7 @@ package net.aminecraftdev.custombosses.mechanics;
import net.aminecraftdev.custombosses.entity.BossEntity;
import net.aminecraftdev.custombosses.entity.elements.EquipmentElement;
import net.aminecraftdev.custombosses.holder.ActiveBossHolder;
import net.aminecraftdev.custombosses.managers.BossItemFileManager;
import net.aminecraftdev.custombosses.managers.files.BossItemFileManager;
import net.aminecraftdev.custombosses.utils.IMechanic;
import net.aminecraftdev.custombosses.utils.itemstack.holder.ItemStackHolder;
import org.bukkit.entity.LivingEntity;
@ -25,9 +25,9 @@ public class EquipmentMechanic implements IMechanic {
@Override
public boolean applyMechanic(BossEntity bossEntity, ActiveBossHolder activeBossHolder) {
if(activeBossHolder.getLivingEntity() == null) return false;
if(activeBossHolder.getLivingEntityMap().getOrDefault(0, null) == null) return false;
LivingEntity livingEntity = activeBossHolder.getLivingEntity();
LivingEntity livingEntity = activeBossHolder.getLivingEntityMap().getOrDefault(0, null);
EquipmentElement equipmentElement = bossEntity.getEquipment();
EntityEquipment entityEquipment = livingEntity.getEquipment();
String helmet = equipmentElement.getHelmet();

View File

@ -3,7 +3,7 @@ package net.aminecraftdev.custombosses.mechanics;
import net.aminecraftdev.custombosses.entity.BossEntity;
import net.aminecraftdev.custombosses.entity.elements.HandsElement;
import net.aminecraftdev.custombosses.holder.ActiveBossHolder;
import net.aminecraftdev.custombosses.managers.BossItemFileManager;
import net.aminecraftdev.custombosses.managers.files.BossItemFileManager;
import net.aminecraftdev.custombosses.utils.IMechanic;
import net.aminecraftdev.custombosses.utils.version.VersionHandler;
import net.aminecraftdev.custombosses.utils.itemstack.holder.ItemStackHolder;
@ -28,9 +28,9 @@ public class WeaponMechanic implements IMechanic {
@Override
public boolean applyMechanic(BossEntity bossEntity, ActiveBossHolder activeBossHolder) {
if(activeBossHolder.getLivingEntity() == null) return false;
if(activeBossHolder.getLivingEntityMap().getOrDefault(0, null) == null) return false;
LivingEntity livingEntity = activeBossHolder.getLivingEntity();
LivingEntity livingEntity = activeBossHolder.getLivingEntityMap().getOrDefault(0, null);
EntityEquipment entityEquipment = livingEntity.getEquipment();
HandsElement handsElement = bossEntity.getHands();
String mainHand = handsElement.getMainHand();

View File

@ -8,11 +8,12 @@ package net.aminecraftdev.custombosses.utils;
public enum Debug {
NULL_CHECK("An object was found as null when it should not be null."),
NULL_ENTITY_TYPE("The {0} boss or minion has got an invalid entity type."),
MAX_HEALTH("You cannot set the max health higher than {0}. You can adjust your max health in the spigot.yml file and restart your server to increase this."),
MECHANIC_APPLICATION_FAILED("Some mechanics have failed to be applied. It got stuck at {0} mechanic."),
BOSS_NAME_EXISTS("A boss was attempted to be created with the name {0} but there is already a boss with that name."),
ATTEMPTED_TO_UPDATE_PLUGIN("Something has attempted to update the PLUGIN variable in the BossAPI class while it is already initialized."),
ATTEMPTED_TO_SPAWN_WHILE_DISABLED("The {0} boss/minion attempted to spawn while editing is enabled."),
FAILED_ATTEMPT_TO_SPAWN_BOSS("A boss has attempted to spawn but cannot spawn for the following reason: \n{0}");

View File

@ -0,0 +1,40 @@
package net.aminecraftdev.custombosses.utils.file;
import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
/**
* @author Charles Cullen
* @version 1.0.0
* @since 01-Jul-18
*/
public abstract class FileHandler<T> implements IFileHandler<T> {
private final JavaPlugin javaPlugin;
private final boolean saveResource;
@Getter private final File file;
public FileHandler(JavaPlugin javaPlugin, boolean saveResource, File file) {
this.javaPlugin = javaPlugin;
this.saveResource = saveResource;
this.file = file;
}
public void createFile() {
if(!this.file.exists()) {
if(this.saveResource) {
String path = this.file.getName();
if(this.javaPlugin.getResource(path) != null) {
this.javaPlugin.saveResource(path, false);
return;
}
}
FileUtils.get().createFile(this.file);
}
}
}

View File

@ -19,7 +19,7 @@
</modules>
<properties>
<plugin.version>3.0.0-SNAPSHOT-U17</plugin.version>
<plugin.version>3.0.0-SNAPSHOT-U18</plugin.version>
<plugin.name>CustomBosses</plugin.name>
<plugin.main>net.aminecraftdev.custombosses.CustomBosses</plugin.main>
<plugin.author>AMinecraftDev</plugin.author>