First Magic System Push

This commit is contained in:
Kam committed 2024-08-23 14:12:59 -04:00
1 parent 934cc3d7cd
commit 5f6aa1a9bd
6 files changed
+276 -7

No files matched your search

@@ -24,23 +24,29 @@ public class SubGuiNpcResistanceProperties extends SubGuiInterface implements IS
addLabel(new GuiNpcLabel(1,"item.arrow.name", guiLeft + 4, guiTop + 37));
addSlider(new GuiNpcSlider(this, 1, guiLeft + 94, guiTop + 32, (int)(resistances.arrow * 100 - 100) + "%", resistances.arrow / 2));
getSlider(1).enabled = !resistances.disableDamage;
addLabel(new GuiNpcLabel(2,"stats.melee", guiLeft + 4, guiTop + 59));
addSlider(new GuiNpcSlider(this, 2, guiLeft + 94, guiTop + 54, (int)(resistances.playermelee * 100 - 100) + "%", resistances.playermelee / 2));
getSlider(2).enabled = !resistances.disableDamage;
addLabel(new GuiNpcLabel(3,"stats.explosion", guiLeft + 4, guiTop + 81));
addSlider(new GuiNpcSlider(this, 3, guiLeft + 94, guiTop + 76, (int)(resistances.explosion * 100 - 100) + "%", resistances.explosion / 2));
getSlider(3).enabled = !resistances.disableDamage;
if(resistances.disableDamage){
addLabel(new GuiNpcLabel(4,"stats.disabletrue", guiLeft + 4, guiTop + 103));
}
addLabel(new GuiNpcLabel(21,"stats.disabledamage", guiLeft + 4, guiTop + 104));
this.addButton(new GuiNpcButton(21, guiLeft + 94, guiTop + 99, 56, 20, new String[]{"gui.no","gui.yes"}, this.resistances.disableDamage? 1:0));
addButton(new GuiNpcButton(66, guiLeft + 190, guiTop + 190, 60, 20, "gui.done"));
}
protected void actionPerformed(GuiButton guibutton)
{
int id = guibutton.id;
if(id == 21){
resistances.disableDamage = ((GuiNpcButton)guibutton).getValue() == 1;
initGui();
}
if(id == 66)
{
close();
@@ -55,7 +61,7 @@ public class SubGuiNpcResistanceProperties extends SubGuiInterface implements IS
@Override
public void mousePressed(GuiNpcSlider slider) {
// TODO Auto-generated method stub
}
@Override
@@ -72,7 +78,7 @@ public class SubGuiNpcResistanceProperties extends SubGuiInterface implements IS
if(slider.id == 3){
resistances.explosion = slider.sliderValue * 2;
}
}
}
@@ -135,6 +135,7 @@ public class GuiNpcStats extends GuiNPCInterface2 implements ITextfieldListener,
stats.potionImmune = ((GuiNpcButtonYesNo)guibutton).getBoolean();
}
else if(button.id == 21){
// Magic Button
stats.resistances.disableDamage = ((GuiNpcButton)guibutton).getValue() == 1;
}
else if (button.id == 22) {
@@ -7,4 +7,5 @@ public class SyncType {
public static final int DIALOG = 4;
public static final int DIALOG_CATEGORY = 5;
public static final int PLAYER_DATA = 6;
public static final int MAGIC = 7;
}
@@ -0,0 +1,207 @@
package noppes.npcs.controllers;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.CustomNpcs;
import noppes.npcs.LogWriter;
import noppes.npcs.Server;
import noppes.npcs.api.handler.IMagicHandler;
import noppes.npcs.api.handler.data.IFaction;
import noppes.npcs.api.handler.data.IMagic;
import noppes.npcs.constants.EnumPacketClient;
import noppes.npcs.constants.SyncType;
import noppes.npcs.controllers.data.Faction;
import noppes.npcs.controllers.data.Magic;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
public class MagicController {
public HashMap<Integer, Magic> magicSync = new HashMap<Integer,Magic>();
public HashMap<Integer,Magic> magics;
private static MagicController instance = new MagicController();
private int lastUsedID = 0;
public MagicController(){
instance = this;
magics = new HashMap<Integer, Magic>();
}
public static MagicController getInstance(){
return instance;
}
public Magic getMagic(int magic) {
return magics.get(magic);
}
public void load(){
magics = new HashMap<Integer, Magic>();
lastUsedID = 0;
File saveDir = CustomNpcs.getWorldSaveDirectory();
if(saveDir == null){
return;
}
try {
File file = new File(saveDir, "magic.dat");
if(file.exists()){
loadMagicFile(file);
}
} catch (Exception e) {
try {
File file = new File(saveDir, "magic.dat_old");
if(file.exists()){
loadMagicFile(file);
}
} catch (Exception ee) {
}
}
if(magics.isEmpty()){
magics.put(0,new Magic(0,"Nature", 0x00DD00));
magics.put(1,new Magic(1,"Arcane", 0xF2DD00));
magics.put(2,new Magic(2,"Ice", 0xDD0000));
magics.put(2,new Magic(3,"Fire", 0xDD0000));
magics.put(2,new Magic(4,"Dark", 0xDD0000));
magics.put(2,new Magic(5,"Holy", 0xDD0000));
}
}
private void loadMagicFile(File file) throws IOException{
DataInputStream var1 = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(file))));
loadMagic(var1);
var1.close();
}
public void loadMagic(DataInputStream stream) throws IOException{
HashMap<Integer,Magic> magic = new HashMap<Integer,Magic>();
NBTTagCompound nbttagcompound1 = CompressedStreamTools.read(stream);
lastUsedID = nbttagcompound1.getInteger("lastID");
NBTTagList list = nbttagcompound1.getTagList("NPCMagic", 10);
if(list != null){
for(int i = 0; i < list.tagCount(); i++)
{
NBTTagCompound nbttagcompound = list.getCompoundTagAt(i);
Magic loadMagic = new Magic();
loadMagic.readNBT(nbttagcompound);
magic.put(loadMagic.id,loadMagic);
}
}
this.magics = magic;
}
public NBTTagCompound getNBT(){
NBTTagList list = new NBTTagList();
for(int slot : magics.keySet()){
Magic mag = magics.get(slot);
NBTTagCompound nbtfactions = new NBTTagCompound();
mag.writeNBT(nbtfactions);
list.appendTag(nbtfactions);
}
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setInteger("lastID", lastUsedID);
nbttagcompound.setTag("NPCFactions", list);
return nbttagcompound;
}
public void saveFactions(){
try {
File saveDir = CustomNpcs.getWorldSaveDirectory();
File file = new File(saveDir, "magic.dat_new");
File file1 = new File(saveDir, "magic.dat_old");
File file2 = new File(saveDir, "magic.dat");
CompressedStreamTools.writeCompressed(getNBT(), new FileOutputStream(file));
if(file1.exists())
{
file1.delete();
}
file2.renameTo(file1);
if(file2.exists())
{
file2.delete();
}
file.renameTo(file2);
if(file.exists())
{
file.delete();
}
} catch (Exception e) {
LogWriter.except(e);
}
}
public Magic get(int magicID) {
return magics.get(magicID);
}
public List<IMagic> list() {
return new ArrayList(this.magics.values());
}
public void saveMagic(Magic mag) {
if(mag.id < 0){
mag.id = getUnusedId();
while(hasName(mag.name))
mag.name += "_";
}
else{
Magic existing = magics.get(mag.id);
if(existing != null && !existing.name.equals(mag.name))
while(hasName(mag.name))
mag.name += "_";
}
magics.remove(mag.id);
magics.put(mag.id, mag);
NBTTagCompound facCompound = new NBTTagCompound();
mag.writeNBT(facCompound);
Server.sendToAll(EnumPacketClient.SYNC_UPDATE, SyncType.MAGIC, facCompound);
saveFactions();
}
public int getUnusedId(){
if(lastUsedID == 0){
for(int catid : magics.keySet())
if(catid > lastUsedID)
lastUsedID = catid;
}
lastUsedID++;
return lastUsedID;
}
public boolean hasName(String newName) {
if(newName.trim().isEmpty())
return true;
for(Magic mag : magics.values())
if(mag.name.equals(newName))
return true;
return false;
}
public Magic getMagicFromName(String magicName){
for (Map.Entry<Integer,Magic> entryMag: MagicController.getInstance().magics.entrySet()){
if (entryMag.getValue().name.equalsIgnoreCase(magicName)){
return entryMag.getValue();
}
}
return null;
}
public String[] getNames() {
String[] names = new String[magics.size()];
int i = 0;
for(Magic mag : magics.values()){
names[i] = mag.name.toLowerCase();
i++;
}
return names;
}
}
@@ -0,0 +1,54 @@
package noppes.npcs.controllers.data;
import net.minecraft.nbt.NBTTagCompound;
import noppes.npcs.api.handler.data.IMagic;
import noppes.npcs.controllers.MagicController;
public class Magic implements IMagic {
public String name = "";
public int color = Integer.parseInt("FF00", 16);
public int id = -1;
public Magic(){}
public Magic(int id, String name, int color){
this.name = name;
this.color = color;
this.id = id;
}
public static String formatName(String name){
name = name.toLowerCase().trim();
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
public void readNBT(NBTTagCompound compound){
name = compound.getString("Name");
color = compound.getInteger("Color");
id = compound.getInteger("Slot");
}
public void writeNBT(NBTTagCompound compound){
compound.setInteger("Slot", id);
compound.setString("Name", name);
compound.setInteger("Color", color);
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setName(String name) { this.name = name; }
public void setColor(int color) { this.color = color; }
public int getColor() {
return this.color;
}
public void save() {
MagicController.getInstance().saveMagic(this);
}
}