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());
}
}
}