mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-13 01:57:34 +00:00
Linked Item [WIP]
This commit is contained in:
1 parent
d45e550321
commit
29e182e82d
2 files changed
+272
No files matched your search
@@ -0,0 +1,114 @@
|
||||
package noppes.npcs.controllers;
|
||||
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.LogWriter;
|
||||
import noppes.npcs.util.JsonException;
|
||||
import noppes.npcs.util.NBTJsonUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LinkedScriptedItemController {
|
||||
public static LinkedScriptedItemController Instance;
|
||||
public List<LinkedScriptedItemData> list = new ArrayList<>();
|
||||
|
||||
public LinkedScriptedItemController() {
|
||||
Instance = this;
|
||||
load();
|
||||
}
|
||||
|
||||
public File getDir() {
|
||||
File dir = new File(CustomNpcs.getWorldSaveDirectory(), "linkeditems");
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
File dir = getDir();
|
||||
File[] files = dir.listFiles((d, name) -> name.endsWith(".json"));
|
||||
if(files == null) return;
|
||||
for (File file : files) {
|
||||
try {
|
||||
NBTTagCompound compound = NBTJsonUtil.LoadFile(file);
|
||||
LinkedScriptedItemData data = new LinkedScriptedItemData();
|
||||
data.loadFromNBT(compound);
|
||||
list.add(data);
|
||||
} catch (Exception e) {
|
||||
LogWriter.error("Error loading linked items: " + file.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
File dir = getDir();
|
||||
for (LinkedScriptedItemData data : list) {
|
||||
try {
|
||||
File file = new File(dir, data.name + ".json_new");
|
||||
NBTJsonUtil.SaveFile(file, data.saveToNBT());
|
||||
} catch (IOException e) {
|
||||
LogWriter.except(e);
|
||||
} catch (JsonException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public LinkedScriptedItemData getData(String name) {
|
||||
for (LinkedScriptedItemData data : list) {
|
||||
if (data.name.equalsIgnoreCase(name)) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addData(String name, NBTTagCompound scriptData) {
|
||||
if (name.isEmpty() || getData(name) != null)
|
||||
return;
|
||||
LinkedScriptedItemData data = new LinkedScriptedItemData();
|
||||
data.name = name;
|
||||
data.scriptData = scriptData;
|
||||
data.time = System.currentTimeMillis();
|
||||
list.add(data);
|
||||
save();
|
||||
}
|
||||
|
||||
public void updateData(String name, NBTTagCompound scriptData) {
|
||||
LinkedScriptedItemData data = getData(name);
|
||||
if (data != null) {
|
||||
data.scriptData = scriptData;
|
||||
data.time = System.currentTimeMillis();
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
public static class LinkedScriptedItemData {
|
||||
public String name = "LinkedScriptedItem";
|
||||
public long time;
|
||||
public NBTTagCompound scriptData = new NBTTagCompound();
|
||||
|
||||
public LinkedScriptedItemData() {
|
||||
time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void loadFromNBT(NBTTagCompound compound) {
|
||||
name = compound.getString("LinkedScriptName");
|
||||
scriptData = compound.getCompoundTag("ScriptData");
|
||||
time = compound.getLong("Time");
|
||||
}
|
||||
|
||||
public NBTTagCompound saveToNBT() {
|
||||
NBTTagCompound compound = new NBTTagCompound();
|
||||
compound.setString("LinkedScriptName", name);
|
||||
compound.setTag("ScriptData", scriptData);
|
||||
compound.setLong("Time", time);
|
||||
return compound;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package noppes.npcs.items;
|
||||
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.EnumAction;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ChatComponentTranslation;
|
||||
import net.minecraft.world.World;
|
||||
import noppes.npcs.CustomItems;
|
||||
import noppes.npcs.CustomNpcs;
|
||||
import noppes.npcs.CustomNpcsPermissions;
|
||||
import noppes.npcs.EventHooks;
|
||||
import noppes.npcs.api.item.IItemStack;
|
||||
import noppes.npcs.config.ConfigScript;
|
||||
import noppes.npcs.constants.EnumGuiType;
|
||||
import noppes.npcs.scripted.NpcAPI;
|
||||
import noppes.npcs.scripted.event.ItemEvent;
|
||||
import noppes.npcs.scripted.item.ScriptCustomItem;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
public class ItemLinked extends Item implements ItemRenderInterface {
|
||||
|
||||
public ItemLinked() {
|
||||
maxStackSize = 1;
|
||||
CustomNpcs.proxy.registerItem(this);
|
||||
setHasSubtypes(true);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public void registerIcons(IIconRegister par1IconRegister){
|
||||
this.itemIcon = Items.iron_pickaxe.getIconFromDamage(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColorFromItemStack(ItemStack itemStack, int par2){
|
||||
return 0x8B4513;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requiresMultipleRenderPasses(){
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item setUnlocalizedName(String name){
|
||||
GameRegistry.registerItem(this, name);
|
||||
return super.setUnlocalizedName(name);
|
||||
}
|
||||
|
||||
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
|
||||
{
|
||||
player.setItemInUse(stack, this.getMaxItemUseDuration(stack));
|
||||
return stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onEntitySwing(EntityLivingBase entityLivingBase, ItemStack stack){
|
||||
if (entityLivingBase.worldObj.isRemote) {
|
||||
return false;
|
||||
}
|
||||
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent( (ScriptCustomItem) istack, NpcAPI.Instance().getIEntity(entityLivingBase), 2, null);
|
||||
return EventHooks.onScriptItemAttack((ScriptCustomItem) istack, eve);
|
||||
}
|
||||
|
||||
public int getMaxItemUseDuration(ItemStack stack){
|
||||
return (new ScriptCustomItem(stack)).getMaxItemUseDuration();
|
||||
}
|
||||
|
||||
public static ScriptCustomItem GetWrapper(ItemStack stack) {
|
||||
return new ScriptCustomItem(stack);
|
||||
}
|
||||
|
||||
public boolean showDurabilityBar(ItemStack stack) {
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
return istack instanceof ScriptCustomItem && (new ScriptCustomItem(stack)).durabilityShow;
|
||||
}
|
||||
|
||||
public double getDurabilityForDisplay(ItemStack stack) {
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
return istack instanceof ScriptCustomItem ? 1.0D - (new ScriptCustomItem(stack)).durabilityValue : 1.0D;
|
||||
}
|
||||
|
||||
public int getItemStackLimit(ItemStack stack) {
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getMaxStackSize() : super.getItemStackLimit(stack);
|
||||
}
|
||||
|
||||
public boolean isItemTool(ItemStack stack)
|
||||
{
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).isTool() : super.isItemTool(stack);
|
||||
}
|
||||
|
||||
public float getDigSpeed(ItemStack stack, Block block, int metadata)
|
||||
{
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getDigSpeed() : super.getDigSpeed(stack, block, metadata);
|
||||
}
|
||||
|
||||
public boolean isValidArmor(ItemStack stack, int armorType, Entity entity)
|
||||
{
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
|
||||
if((new ScriptCustomItem(stack)).getArmorType() == -1)
|
||||
return true;
|
||||
|
||||
return istack instanceof ScriptCustomItem ? armorType == (new ScriptCustomItem(stack)).getArmorType() : super.isValidArmor(stack, armorType, entity);
|
||||
}
|
||||
|
||||
public int getItemEnchantability(ItemStack stack)
|
||||
{
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
return istack instanceof ScriptCustomItem ? (new ScriptCustomItem(stack)).getEnchantability() : super.getItemEnchantability(stack);
|
||||
}
|
||||
|
||||
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) {
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
ItemEvent.AttackEvent eve = new ItemEvent.AttackEvent( (ScriptCustomItem) istack, NpcAPI.Instance().getIEntity(attacker), 1, NpcAPI.Instance().getIEntity(target));
|
||||
return EventHooks.onScriptItemAttack((ScriptCustomItem) istack, eve);
|
||||
}
|
||||
|
||||
public EnumAction getItemUseAction(ItemStack stack)
|
||||
{
|
||||
IItemStack istack = NpcAPI.Instance().getIItemStack(stack);
|
||||
if (istack instanceof ScriptCustomItem) {
|
||||
switch (((ScriptCustomItem) istack).getItemUseAction()) {
|
||||
case 0:
|
||||
return EnumAction.none;
|
||||
case 1:
|
||||
return EnumAction.block;
|
||||
case 2:
|
||||
return EnumAction.bow;
|
||||
case 3:
|
||||
return EnumAction.eat;
|
||||
case 4:
|
||||
return EnumAction.drink;
|
||||
}
|
||||
}
|
||||
return super.getItemUseAction(stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderSpecial() {}
|
||||
|
||||
public void renderOffset(ScriptCustomItem scriptCustomItem) {
|
||||
GL11.glTranslatef(0.135F * scriptCustomItem.scaleX, 0.2F * scriptCustomItem.scaleY, 0.07F * scriptCustomItem.scaleZ);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user