Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/data/CustomEffect.java
T

334 lines
9.0 KiB
Java
Raw Normal View History

2025-02-10 14:58:45 -05:00
package noppes.npcs.controllers.data;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.Constants;
2025-03-14 03:33:10 -04:00
import noppes.npcs.EventHooks;
2025-02-10 14:58:45 -05:00
import noppes.npcs.NoppesUtilServer;
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.handler.data.ICustomEffect;
2025-02-10 17:24:26 -05:00
import noppes.npcs.controllers.CustomEffectController;
2025-04-08 22:37:26 -04:00
import noppes.npcs.scripted.event.player.PlayerEvent;
2025-02-10 14:58:45 -05:00
import java.util.function.Consumer;
public class CustomEffect implements ICustomEffect {
public int id = -1;
public String name = "";
public boolean lossOnDeath = true;
public int length = 30;
// Must be a multiple of 10
public int everyXTick = 20;
public String icon = "";
public int iconX = 0, iconY = 0;
/**
* Experimental script stuff.
*/
public Consumer<PlayerEvent.EffectEvent.Added> onAddedConsumer;
public Consumer<PlayerEvent.EffectEvent.Ticked> onTickConsumer;
public Consumer<PlayerEvent.EffectEvent.Removed> onRemovedConsumer;
public String menuName = "§aNEW EFFECT";
public int width = 16, height = 16;
public int index = 0;
2025-03-13 17:14:45 -04:00
public CustomEffect() {
}
2025-02-10 14:58:45 -05:00
public CustomEffect(int id) {
this.id = id;
}
public CustomEffect(int id, String name) {
this(id);
this.name = name;
}
@Override
public int getID() {
return this.id;
}
@Override
public String getName() {
return this.name;
}
@Override
public void setMenuName(String name) {
if (name != null && !name.isEmpty())
this.menuName = name.replaceAll("&", "§");
}
@Override
public String getMenuName() {
return menuName;
}
@Override
public void setName(String name) {
if (name != null && !name.isEmpty())
this.name = name;
}
@Override
public String getIcon() {
return icon;
}
@Override
public void setIcon(String icon) {
this.icon = icon;
}
@Override
public int getEveryXTick() {
return everyXTick;
}
@Override
public void setEveryXTick(int everyXTick) {
if (everyXTick < 10)
everyXTick = 10;
int remainder = everyXTick % 10;
if (remainder >= 5)
everyXTick += 10 - remainder;
else
everyXTick -= remainder;
this.everyXTick = everyXTick;
}
@Override
public int getIconX() {
return iconX;
}
@Override
public void setIconX(int iconX) {
this.iconX = iconX;
}
@Override
public int getIconY() {
return iconY;
}
@Override
public void setIconY(int iconY) {
this.iconY = iconY;
}
@Override
public int getWidth() {
return this.width;
}
@Override
public int getHeight() {
return this.height;
}
@Override
public void setWidth(int width) {
this.width = width;
}
@Override
public void setHeight(int height) {
this.height = height;
}
@Override
public boolean isLossOnDeath() {
return lossOnDeath;
}
@Override
public void setLossOnDeath(boolean lossOnDeath) {
this.lossOnDeath = lossOnDeath;
}
@Override
public ICustomEffect save() {
2025-02-10 17:24:26 -05:00
return CustomEffectController.getInstance().saveEffect(this);
2025-02-10 14:58:45 -05:00
}
@Override
public void setID(int id) {
this.id = id;
}
@Override
public int getIndex() {
return this.index;
}
public void onAdded(Consumer<PlayerEvent.EffectEvent.Added> function) {
onAddedConsumer = function;
}
public void onTick(Consumer<PlayerEvent.EffectEvent.Ticked> function) {
onTickConsumer = function;
}
public void onRemoved(Consumer<PlayerEvent.EffectEvent.Removed> function) {
onRemovedConsumer = function;
}
public void onAdded(EntityPlayer player, PlayerEffect playerEffect) {
IPlayer iPlayer = NoppesUtilServer.getIPlayer(player);
2025-03-14 03:33:10 -04:00
if(playerEffect.index == 0){
PlayerEvent.EffectEvent.Added event = new PlayerEvent.EffectEvent.Added(iPlayer, playerEffect);
2025-02-10 14:58:45 -05:00
2025-03-14 03:33:10 -04:00
if (onAddedConsumer != null)
onAddedConsumer.accept(event);
EffectScript script = getScriptHandler();
if (script == null) {
return;
}
script.callScript(EffectScript.ScriptType.OnEffectAdd, event);
2025-02-10 14:58:45 -05:00
}
2025-03-14 03:33:10 -04:00
EventHooks.onEffectAdded(iPlayer, playerEffect);
2025-02-10 14:58:45 -05:00
}
public void onTick(EntityPlayer player, PlayerEffect playerEffect) {
IPlayer iPlayer = NoppesUtilServer.getIPlayer(player);
2025-03-14 03:33:10 -04:00
if(playerEffect.index == 0){
PlayerEvent.EffectEvent.Ticked event = new PlayerEvent.EffectEvent.Ticked(iPlayer, playerEffect);
2025-02-10 14:58:45 -05:00
2025-03-14 03:33:10 -04:00
if (onTickConsumer != null) {
onTickConsumer.accept(event);
}
2025-02-10 14:58:45 -05:00
2025-03-14 03:33:10 -04:00
EffectScript script = getScriptHandler();
if (script == null) {
return;
}
script.callScript(EffectScript.ScriptType.OnEffectTick, event);
2025-02-10 14:58:45 -05:00
}
2025-03-14 03:33:10 -04:00
EventHooks.onEffectTick(iPlayer, playerEffect);
2025-02-10 14:58:45 -05:00
}
public void onRemoved(EntityPlayer player, PlayerEffect playerEffect, PlayerEvent.EffectEvent.ExpirationType type) {
IPlayer iPlayer = NoppesUtilServer.getIPlayer(player);
2025-03-14 03:33:10 -04:00
if(playerEffect.index == 0){
PlayerEvent.EffectEvent.Removed event = new PlayerEvent.EffectEvent.Removed(iPlayer, playerEffect, type);
2025-02-10 14:58:45 -05:00
2025-03-14 03:33:10 -04:00
if (onRemovedConsumer != null) {
onRemovedConsumer.accept(event);
}
EffectScript script = getScriptHandler();
if (script == null) {
return;
}
2025-02-10 14:58:45 -05:00
2025-03-14 03:33:10 -04:00
script.callScript(EffectScript.ScriptType.OnEffectRemove, event);
2025-02-10 14:58:45 -05:00
}
2025-03-14 03:33:10 -04:00
EventHooks.onEffectRemove(iPlayer, playerEffect, type);
2025-02-10 14:58:45 -05:00
}
public NBTTagCompound writeToNBT(boolean saveScripts) {
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("ID", id);
compound.setString("name", name);
compound.setString("menuName", menuName);
compound.setInteger("length", length);
compound.setInteger("everyXTick", everyXTick);
compound.setInteger("iconX", iconX);
compound.setInteger("iconY", iconY);
compound.setInteger("iconWidth", width);
compound.setInteger("iconHeight", height);
compound.setString("icon", icon);
compound.setBoolean("lossOnDeath", lossOnDeath);
// if (saveScripts && scriptContainer != null) {
if (saveScripts) {
NBTTagCompound scriptData = new NBTTagCompound();
2025-02-11 13:56:28 -05:00
EffectScript handler = getScriptHandler();
2025-02-10 14:58:45 -05:00
if (handler != null)
handler.writeToNBT(scriptData);
compound.setTag("ScriptData", scriptData);
}
return compound;
}
public void readFromNBT(NBTTagCompound compound) {
if (compound.hasKey("ID"))
id = compound.getInteger("ID");
else
2025-02-10 17:24:26 -05:00
id = CustomEffectController.Instance.getUnusedId();
2025-02-10 14:58:45 -05:00
name = compound.getString("name");
if (compound.hasKey("menuName", Constants.NBT.TAG_STRING))
menuName = compound.getString("menuName");
else
menuName = name;
length = compound.getInteger("length");
everyXTick = compound.getInteger("everyXTick");
iconX = compound.getInteger("iconX");
iconY = compound.getInteger("iconY");
if (compound.hasKey("iconWidth", Constants.NBT.TAG_INT))
width = compound.getInteger("iconWidth");
else
width = 16;
if (compound.hasKey("iconHeight", Constants.NBT.TAG_INT))
height = compound.getInteger("iconHeight");
else
height = 16;
icon = compound.getString("icon");
lossOnDeath = compound.getBoolean("lossOnDeath");
if (compound.hasKey("ScriptData", Constants.NBT.TAG_COMPOUND)) {
2025-02-11 13:56:28 -05:00
EffectScript handler = new EffectScript();
2025-02-10 14:58:45 -05:00
handler.readFromNBT(compound.getCompoundTag("ScriptData"));
setScriptHandler(handler);
}
}
public CustomEffect cloneEffect() {
CustomEffect newEffect = new CustomEffect();
newEffect.readFromNBT(this.writeToNBT(true));
newEffect.id = -1;
return newEffect;
}
2025-02-11 13:56:28 -05:00
public EffectScript getScriptHandler() {
2025-02-10 17:24:26 -05:00
return CustomEffectController.getInstance().customEffectScriptHandlers.get(this.id);
2025-02-10 14:58:45 -05:00
}
2025-03-13 17:14:45 -04:00
2025-02-11 13:56:28 -05:00
public void setScriptHandler(EffectScript handler) {
2025-02-10 17:24:26 -05:00
CustomEffectController.getInstance().customEffectScriptHandlers.put(this.id, handler);
2025-02-10 14:58:45 -05:00
}
2025-02-11 13:56:28 -05:00
public EffectScript getOrCreateScriptHandler() {
EffectScript data = getScriptHandler();
2025-02-10 14:58:45 -05:00
if (data == null)
2025-03-13 17:14:45 -04:00
setScriptHandler(data = new EffectScript());
2025-02-10 14:58:45 -05:00
return data;
}
public void runEffect(EntityPlayer player, PlayerEffect playerEffect) {
if (player.ticksExisted % everyXTick == 0) {
onTick(player, playerEffect);
}
}
}