Created
January 29, 2018 02:01
-
-
Save Daomephsta/200501bb5881966ca63fff381403afe5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package leviathan143.dogwhistle.common.capability; | |
| import java.util.concurrent.Callable; | |
| import leviathan143.dogwhistle.common.capability.PackHandler.Pack; | |
| import net.minecraft.entity.EntityLivingBase; | |
| import net.minecraft.nbt.NBTBase; | |
| import net.minecraft.util.EnumFacing; | |
| import net.minecraftforge.common.capabilities.*; | |
| public class CapabilityPack | |
| { | |
| @CapabilityInject(Pack.class) | |
| public static Capability<Pack> CAP_PACK = null; | |
| public static void register() | |
| { | |
| CapabilityManager.INSTANCE.register(Pack.class, new Capability.IStorage<Pack>() | |
| { | |
| @Override | |
| public NBTBase writeNBT(Capability<Pack> capability, Pack instance, EnumFacing side) | |
| { | |
| return null; | |
| } | |
| @Override | |
| public void readNBT(Capability<Pack> capability, Pack instance, EnumFacing side, NBTBase nbt) | |
| {} | |
| }, new Callable<Pack>() | |
| { | |
| @Override | |
| public Pack call() throws Exception | |
| { | |
| return null; | |
| } | |
| }); | |
| } | |
| public static Pack getPack(EntityLivingBase entity) | |
| { | |
| return entity.getCapability(CapabilityPack.CAP_PACK, null); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package leviathan143.dogwhistle.common.capability; | |
| import java.util.*; | |
| import leviathan143.dogwhistle.common.DogWhistleMain.Constants; | |
| import leviathan143.dogwhistle.common.lib.CyclicListIterator; | |
| import leviathan143.dogwhistle.common.lib.WeakArrayList; | |
| import net.minecraft.entity.*; | |
| import net.minecraft.entity.player.EntityPlayer; | |
| import net.minecraft.nbt.NBTTagList; | |
| import net.minecraft.nbt.NBTUtil; | |
| import net.minecraft.util.EnumFacing; | |
| import net.minecraft.util.ResourceLocation; | |
| import net.minecraft.util.text.TextComponentTranslation; | |
| import net.minecraft.world.World; | |
| import net.minecraft.world.WorldServer; | |
| import net.minecraftforge.common.capabilities.Capability; | |
| import net.minecraftforge.common.capabilities.ICapabilitySerializable; | |
| import net.minecraftforge.common.util.INBTSerializable; | |
| public class PackHandler implements ICapabilitySerializable<NBTTagList> | |
| { | |
| public static final ResourceLocation CAP_KEY = new ResourceLocation(Constants.MODID, "pack"); | |
| private Pack pack; | |
| public PackHandler(EntityLivingBase leader) | |
| { | |
| this.pack = new Pack(leader.getEntityWorld(), (EntityPlayer) leader); | |
| } | |
| @Override | |
| public boolean hasCapability(Capability<?> capability, EnumFacing facing) | |
| { | |
| if (capability == CapabilityPack.CAP_PACK) | |
| { | |
| return true; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public <T> T getCapability(Capability<T> capability, EnumFacing facing) | |
| { | |
| if (capability == CapabilityPack.CAP_PACK) return CapabilityPack.CAP_PACK.cast(pack); | |
| return null; | |
| } | |
| @Override | |
| public NBTTagList serializeNBT() | |
| { | |
| return pack.serializeNBT(); | |
| } | |
| @Override | |
| public void deserializeNBT(NBTTagList nbt) | |
| { | |
| pack.deserializeNBT(nbt); | |
| } | |
| public static class Pack implements INBTSerializable<NBTTagList> | |
| { | |
| private World world; | |
| private EntityPlayer player; | |
| private List<EntityLiving> members = new WeakArrayList<>(); | |
| private ListIterator<EntityLiving> cyclicIterator = new CyclicListIterator<EntityLiving>(members); | |
| private EntityLiving selectedMember; | |
| public Pack(World world, EntityPlayer player) | |
| { | |
| this.world = world; | |
| this.player = player; | |
| } | |
| public boolean addPackMember(EntityLiving entity) | |
| { | |
| return addPackMember(entity, true); | |
| } | |
| public boolean addPackMember(EntityLiving entity, boolean notifyPlayer) | |
| { | |
| if (!entity.hasCapability(CapabilityWhistleableEntity.CAP_WHISTLEABLE_ENTITY, null)) return false; | |
| if (!CapabilityWhistleableEntity.getWhistleCap(entity).isOwnedByPlayer(entity, player)) return false; | |
| if (members.contains(entity)) return false; | |
| boolean success = members.add(entity); | |
| if (success && notifyPlayer) | |
| player.sendStatusMessage( | |
| new TextComponentTranslation(Constants.MODID + ".pack.addMember", entity.getName()), true); | |
| return success; | |
| } | |
| public boolean hasMember(EntityLivingBase entity) | |
| { | |
| return members.contains(entity); | |
| } | |
| public boolean removePackMember(EntityLivingBase entity) | |
| { | |
| return removePackMember(entity, true); | |
| } | |
| public boolean removePackMember(EntityLivingBase entity, boolean notifyPlayer) | |
| { | |
| if (!entity.hasCapability(CapabilityWhistleableEntity.CAP_WHISTLEABLE_ENTITY, null)) return false; | |
| if (!members.contains(entity)) return false; | |
| if (selectedMember == entity) selectedMember = null; | |
| boolean success = members.remove(entity); | |
| if (success && notifyPlayer) | |
| { | |
| player.sendStatusMessage( | |
| new TextComponentTranslation(Constants.MODID + ".pack.removeMember", entity.getName()), true); | |
| } | |
| return success; | |
| } | |
| public void setMembers(List<EntityLiving> pack) | |
| { | |
| members = pack; | |
| selectedMember = null; | |
| } | |
| public List<EntityLiving> getMembers() | |
| { | |
| return members; | |
| } | |
| public void nextMember() | |
| { | |
| validate(); | |
| if (cyclicIterator.hasNext()) | |
| { | |
| selectedMember = cyclicIterator.next(); | |
| } | |
| System.out.println(members); | |
| } | |
| public void prevMember() | |
| { | |
| validate(); | |
| if (cyclicIterator.hasPrevious()) | |
| { | |
| selectedMember = cyclicIterator.previous(); | |
| } | |
| System.out.println(members); | |
| } | |
| public EntityLiving getSelectedMember() | |
| { | |
| validate(); | |
| if (selectedMember == null && cyclicIterator.hasNext()) selectedMember = cyclicIterator.next(); | |
| return selectedMember; | |
| } | |
| public void validate() | |
| { | |
| for (Iterator<EntityLiving> iter = members.iterator(); iter.hasNext();) | |
| { | |
| EntityLiving living = iter.next(); | |
| if (living == null || !living.isEntityAlive() || !world.getLoadedEntityList().contains(living)) | |
| { | |
| iter.remove(); | |
| } | |
| } | |
| } | |
| @Override | |
| public NBTTagList serializeNBT() | |
| { | |
| NBTTagList memberList = new NBTTagList(); | |
| for (Entity entity : members) | |
| { | |
| if(entity == null) | |
| { | |
| System.out.println("WTF"); | |
| continue; | |
| } | |
| memberList.appendTag(NBTUtil.createUUIDTag(entity.getPersistentID())); | |
| } | |
| return memberList; | |
| } | |
| @Override | |
| public void deserializeNBT(NBTTagList nbt) | |
| { | |
| if (world instanceof WorldServer) | |
| { | |
| for (int m = 0; m < nbt.tagCount(); m++) | |
| { | |
| members.add((EntityLiving) ((WorldServer) world) | |
| .getEntityFromUuid(NBTUtil.getUUIDFromTag(nbt.getCompoundTagAt(m)))); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment