Posted December 23, 20168 yr Hey guys! I've been trying to understand how Capabilities work. I want to create a simple mod that displays a player's coordinates when given a command, and a player's death coordinates when given that same command with one argument. So far I've gotten the first part down perfectly. The second part has been difficult because I want to store the death coordinates into a capability and then retrieve it when the command is used. The code in it's current state crashes completely. I've been following this tutorial as a guideline: http://www.planetminecraft.com/blog/forge-tutorial-capability-system/ Hopefully someone here can help figure out what's wrong with my code. I'd love to be able to understand what's going wrong. Capabilities seem extremely useful and I'd love to be able to learn how to use them. I know there's a lot to share below, but I can't pinpoint at what location my capability setup is incorrect. I appreciate any and all criticism that will help me improve! Here's my code: main package blazebeetle.mod; import blazebeetle.capabilities.Ideathloc; import blazebeetle.capabilities.deathHandler; import blazebeetle.capabilities.deathProvider; import blazebeetle.commands.coords; import net.minecraft.command.ServerCommandManager; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.entity.living.LivingDeathEvent; import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.event.FMLServerStartingEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent; import net.minecraftforge.fml.common.network.NetworkRegistry; @Mod(modid = BlazeBeetle.MODID, version = BlazeBeetle.VERSION) public class BlazeBeetle { public static final String MODID = "blazebeetle"; public static final String VERSION = "0.0.0"; @EventHandler public void serverStart(FMLServerStartingEvent event){ ServerCommandManager manager = (ServerCommandManager) event.getServer().getCommandManager(); manager.registerCommand(new coords()); MinecraftForge.EVENT_BUS.register(new deathHandler()); } @SubscribeEvent public void onPlayerLogsIn(PlayerLoggedInEvent event){ System.out.println("Logged in!!!!!!"); EntityPlayer player = event.player; Ideathloc deathloc = player.getCapability(deathProvider.DEATHLOC_CAP, null); } /* @SubscribeEvent public void onPlayerDeath(LivingDeathEvent event){ System.out.println("I DIED"); if (event.getEntity() instanceof EntityPlayer){ EntityPlayer player = (EntityPlayer)event.getEntityLiving(); Ideathloc deathloc = player.getCapability(deathProvider.DEATHLOC_CAP, null); deathloc.setLoc(player.getPositionVector().xCoord, player.getPositionVector().yCoord, player.getPositionVector().zCoord); System.out.println("set new death loc"); } }*/ } coords command class package blazebeetle.commands; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.ListIterator; import blazebeetle.capabilities.Ideathloc; import blazebeetle.capabilities.deathProvider; import jline.internal.Nullable; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.server.MinecraftServer; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextFormatting; public class coords extends CommandBase{ @Override public String getCommandName() { return "coords"; } @Override public String getCommandUsage(ICommandSender sender) { return "Displays the player's location to everyone."; } @Override public int getRequiredPermissionLevel() { return 0; } @Override public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { if (sender instanceof EntityPlayer){ EntityPlayer player = (EntityPlayer)sender; int dim_id = player.dimension; String dim_string = null; switch (dim_id){ case -1: dim_string = "The Nether"; break; case 0: dim_string = "The Overworld"; break; case 1: dim_string = "The End"; break; default: dim_string = "Dimension " + dim_id; } TextComponentString text = new TextComponentString(TextFormatting.YELLOW + "<" + player.getDisplayNameString() + "> I'm at x=" + (int)(player.getPositionVector().xCoord) + " z=" + (int)(player.getPositionVector().zCoord) + " y=" + (int)(player.getPositionVector().yCoord) + " in " + dim_string); if (args.length == 0){ player.addChatComponentMessage(text); } else if (args.length == 1){ System.out.println("inside length 1"); if (args[0].equalsIgnoreCase("death")){ System.out.println("inside death"); Ideathloc deathloc = player.getCapability(deathProvider.DEATHLOC_CAP,null); player.addChatComponentMessage(new TextComponentString(TextFormatting.YELLOW + "I HAVENT CODED THIS YET!!!")); player.addChatComponentMessage(new TextComponentString(TextFormatting.YELLOW + "<" + player.getDisplayNameString() + "> I died at x=" + (int)deathloc.getLoc().xCoord + " z=" + (int)deathloc.getLoc().zCoord + " y=" + (int)deathloc.getLoc().yCoord + " in SOME FUCKING DIMENSION!!!")); return; } String[] playerList = server.getAllUsernames(); for (int i = 0; i < playerList.length; i++){ if (args[0].equalsIgnoreCase(playerList[i])){ EntityPlayer receiver = server.getEntityWorld().getPlayerEntityByName(playerList[i]); receiver.addChatMessage(text); } } } } } @Override public List<String> getTabCompletionOptions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos pos) { return args.length == 0 ? Collections.<String>emptyList() : getListOfStringsMatchingLastWord(args, server.getAllUsernames()); } } Ideathloc package blazebeetle.capabilities; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.math.Vec3d; public interface Ideathloc { public void writeLoc(); public NBTTagCompound getLocComp(); public Vec3d getLoc(); public void setLoc(); public void setLoc(double x, double y, double z); } deathloc package blazebeetle.capabilities; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.math.Vec3d; public class deathloc implements Ideathloc{ private Vec3d location; private NBTTagCompound nbt; double x; double y; double z; public void writeLoc(){ nbt.setDouble("x", this.x); nbt.setDouble("y", this.y); nbt.setDouble("z", this.z); } public NBTTagCompound getLocComp() { return nbt; } public void setLoc() { Vec3d temp = new Vec3d(nbt.getDouble("x"), nbt.getDouble("y"), nbt.getDouble("z")); } @Override public void setLoc(double x, double y, double z) { location = new Vec3d(x, y, z); this.x = x; this.y = y; this.z = z; } @Override public Vec3d getLoc() { return location; } } deathStorage package blazebeetle.capabilities; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.Capability.IStorage; public class deathStorage implements IStorage<Ideathloc>{ @Override public NBTBase writeNBT(Capability<Ideathloc> capability, Ideathloc instance, EnumFacing side) { instance.writeLoc(); return (instance.getLocComp()); } @Override public void readNBT(Capability<Ideathloc> capability, Ideathloc instance, EnumFacing side, NBTBase nbt) { instance.setLoc(); } } deathProvider package blazebeetle.capabilities; import net.minecraft.nbt.NBTBase; import net.minecraft.util.EnumFacing; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityInject; import net.minecraftforge.common.capabilities.ICapabilitySerializable; public class deathProvider implements ICapabilitySerializable<NBTBase>{ @CapabilityInject(Ideathloc.class) public static final Capability<Ideathloc> DEATHLOC_CAP = null; private Ideathloc instance = DEATHLOC_CAP.getDefaultInstance(); @Override public boolean hasCapability(Capability<?> capability, EnumFacing facing) { return capability == DEATHLOC_CAP; } @Override public <T> T getCapability(Capability<T> capability, EnumFacing facing) { return capability == DEATHLOC_CAP ? DEATHLOC_CAP.<T> cast(this.instance) : null; } @Override public NBTBase serializeNBT() { return DEATHLOC_CAP.getStorage().writeNBT(DEATHLOC_CAP, this.instance, null); } @Override public void deserializeNBT(NBTBase nbt) { DEATHLOC_CAP.getStorage().readNBT(DEATHLOC_CAP, this.instance, null, nbt); } } deathHandler package blazebeetle.capabilities; import blazebeetle.mod.BlazeBeetle; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ResourceLocation; import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; public class deathHandler { public static final ResourceLocation DEATHLOC_CAP = new ResourceLocation(BlazeBeetle.MODID, "deathloc"); @SubscribeEvent public void attachCapability(AttachCapabilitiesEvent.Entity event){ if (!(event.getEntity() instanceof EntityPlayer)){ event.addCapability(DEATHLOC_CAP, new deathProvider()); } } }
December 23, 20168 yr Post the crash. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
December 23, 20168 yr Author Post the crash. ---- Minecraft Crash Report ---- Time: 12/22/16 11:25 PM Description: Ticking entity java.lang.NullPointerException: Ticking entity at blazebeetle.capabilities.deathProvider.<init>(deathProvider.java:13) at blazebeetle.capabilities.deathHandler.attachCapability(deathHandler.java:16) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_deathHandler_attachCapability_Entity.invoke(.dynamic) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185) at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:565) at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:550) at net.minecraft.entity.Entity.<init>(Entity.java:252) at net.minecraft.entity.item.EntityItem.<init>(EntityItem.java:54) at net.minecraft.entity.item.EntityItem.<init>(EntityItem.java:67) at net.minecraft.entity.Entity.entityDropItem(Entity.java:2008) at net.minecraft.entity.Entity.dropItemWithOffset(Entity.java:1998) at net.minecraft.entity.Entity.dropItem(Entity.java:1993) at net.minecraft.entity.passive.EntityChicken.onLivingUpdate(EntityChicken.java:108) at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2225) at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:342) at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2111) at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:874) at net.minecraft.world.World.updateEntity(World.java:2078) at net.minecraft.world.World.updateEntities(World.java:1891) at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:646) at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:783) at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:687) at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:536) at java.lang.Thread.run(Thread.java:745) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Server thread Stacktrace: at blazebeetle.capabilities.deathProvider.<init>(deathProvider.java:13) at blazebeetle.capabilities.deathHandler.attachCapability(deathHandler.java:16) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_deathHandler_attachCapability_Entity.invoke(.dynamic) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:185) at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:565) at net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:550) at net.minecraft.entity.Entity.<init>(Entity.java:252) at net.minecraft.entity.item.EntityItem.<init>(EntityItem.java:54) at net.minecraft.entity.item.EntityItem.<init>(EntityItem.java:67) at net.minecraft.entity.Entity.entityDropItem(Entity.java:2008) at net.minecraft.entity.Entity.dropItemWithOffset(Entity.java:1998) at net.minecraft.entity.Entity.dropItem(Entity.java:1993) at net.minecraft.entity.passive.EntityChicken.onLivingUpdate(EntityChicken.java:108) at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2225) at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:342) at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2111) at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:874) at net.minecraft.world.World.updateEntity(World.java:2078) -- Entity being ticked -- Details: Entity Type: Chicken (net.minecraft.entity.passive.EntityChicken) Entity ID: 143 Entity Name: Chicken Entity's Exact location: -655.79, 4.00, 93.89 Entity's Block location: World: (-656,4,93), Chunk: (at 0,0,13 in -41,5; contains blocks -656,0,80 to -641,255,95), Region: (-2,0; contains chunks -64,0 to -33,31, blocks -1024,0,0 to -513,255,511) Entity's Momentum: 0.00, -0.08, 0.00 Entity's Passengers: [] Entity's Vehicle: ~~ERROR~~ NullPointerException: null Stacktrace: at net.minecraft.world.World.updateEntities(World.java:1891) at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:646) -- Affected level -- Details: Level name: New World All players: 0 total; [] Chunk stats: ServerChunkCache: 625 Drop: 0 Level seed: 787196711149034284 Level generator: ID 01 - flat, ver 0. Features enabled: true Level generator options: Level spawn location: World: (-714,4,71), Chunk: (at 6,0,7 in -45,4; contains blocks -720,0,64 to -705,255,79), Region: (-2,0; contains chunks -64,0 to -33,31, blocks -1024,0,0 to -513,255,511) Level time: 12607 game time, 12607 day time Level dimension: 0 Level storage version: 0x04ABD - Anvil Level weather: Rain time: 14053 (now: false), thunder time: 66670 (now: false) Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: true Stacktrace: at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:783) at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:687) at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:536) at java.lang.Thread.run(Thread.java:745) -- System Details -- Details: Minecraft Version: 1.10.2 Operating System: Windows 7 (amd64) version 6.1 Java Version: 1.8.0_111, Oracle Corporation Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation Memory: 677491520 bytes (646 MB) / 1037959168 bytes (989 MB) up to 1037959168 bytes (989 MB) JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0 FML: MCP 9.32 Powered by Forge 12.18.3.2185 4 mods loaded, 4 mods active States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored UCHIJAAAA mcp{9.19} [Minecraft Coder Pack] (minecraft.jar) UCHIJAAAA FML{8.0.99.99} [Forge Mod Loader] (forgeSrc-1.10.2-12.18.3.2185.jar) UCHIJAAAA Forge{12.18.3.2185} [Minecraft Forge] (forgeSrc-1.10.2-12.18.3.2185.jar) UCHIJAAAA blazebeetle{0.0.0} [blaze Beetle] (bin) Loaded coremods (and transformers): GL info: ~~ERROR~~ RuntimeException: No OpenGL context found in the current thread. Profiler Position: N/A (disabled) Player Count: 0 / 8; [] Type: Integrated Server (map_client.txt) Is Modded: Definitely; Client brand changed to 'fml,forge'
December 23, 20168 yr Author Just wanted to let everyone know that I've got it working after changing a lot of the code. I may run into more issues when I push this to multiplayer, but if anyone needs help regarding the solution, just PM me!
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.