Jump to content

[1.9] Custom Capabilities and Networking


MrPlantPots

Recommended Posts

I am wanting to store data throughout the game which can be synchronised with every player, so everyone has the same data. I have been told that using a capability would be the best way to do this. I am just unsure of the procedure involved. Would I attach the capability to the EntityPlayer? If so, how am I meant to store that data on the server side, and how am I meant to synchronise it?

Link to comment
Share on other sites

I've read both of those pages :) I am trying to create a quest book and if one player completes the quest it will be completed for every player. I'm just really confused about how the capability system that is attached to the player stores data server side? I just cant wrap my head around it!

Link to comment
Share on other sites

I tried using WorldSaveData but I was told capabilities were a more suitable choice :(! When I used WorldSaveData I had a few issues as it wasn't saving the data after the game closed? And I needed a way to only initialise the NBT in the WorldSaveData once!

 

I assumed each player would have their own quest progress (or progress shared in a party) like HQM, so I told you to use a capability. If the progress is global, world saved data is the correct choice.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

The party option sounds appealing however, this is only intended for small servers where everybody plays together. I think I'm going to stick with global progress!!!

 

Here was my attempt:

 

 

WorldSaveData class:

public class SPNWorldSavedData extends WorldSavedData {
  private static final String DATA_NAME = "QUESTBOOK";

  private NBTTagCompound data = new NBTTagCompound();
  
  // Required constructors
  public SPNWorldSavedData() {
    super(DATA_NAME);
  }
  public SPNWorldSavedData(String s) {
    super(s);
  }
@Override
public void readFromNBT(NBTTagCompound nbt) {
	// TODO Auto-generated method stub
	data = nbt;

}
@Override
public void writeToNBT(NBTTagCompound nbt) {
	// TODO Auto-generated method stub
	nbt = data;
}

public static SPNWorldSavedData get(World world) {
	  // The IS_GLOBAL constant is there for clarity, and should be simplified into the right branch.
	  MapStorage storage = world.getMapStorage();
	  SPNWorldSavedData instance = (SPNWorldSavedData) storage.loadData(SPNWorldSavedData.class, DATA_NAME);

	  if (instance == null) {
	    instance = new SPNWorldSavedData();
	    storage.setData(DATA_NAME, instance);
	  }
	  return instance;
	}

public NBTTagCompound getData() {
	return data;
}

}

 

Code in world gen to initialise the WorldSaveData:

		SPNWorldSavedData data = SPNWorldSavedData.get(world);
		data.readFromNBT(NBTHandler.getNBTTag());
		data.setDirty(true);

 

Code in event handler to read data:

            SPNWorldSavedData data = SPNWorldSavedData.get(event.getEntityLiving().getEntityWorld());
            NBTTagCompound nbt = new NBTTagCompound();
            nbt = data.getData();
            System.out.println(nbt.getString("obj0name"));
            Minecraft.getMinecraft().thePlayer.sendChatMessage("HIYA: "+nbt.getString("obj0description"));

 

Reference Class (NBTHandler, purely used for organising code):

public class NBTHandler {

static NBTTagCompound nbt;
static ItemStack book;

public static void init() {
	nbt = new NBTTagCompound();
	nbt.setString("obj0name", "NAME0");
	nbt.setString("obj0description", "DESCRIPTION0");
	nbt.setBoolean("obj0state", false);
	nbt.setInteger("obj0QID", 0);


	Block block = SupernaturalBlocks.angel_block;


	book = new ItemStack(SupernaturalItems.blood_bottle, 1, 15);
	System.out.println(book.getItem().getUnlocalizedName());
	book.setTagCompound(nbt);
}

public static NBTTagCompound getNBTTag() {
	return nbt;
}

public static ItemStack getBook() {
	return book;
}

}

Link to comment
Share on other sites

By initialise the WorldSaveData I meant add the NBT tags I created in the other class which I am now going to move into the worldSaveData class. The event it is in is currently an EntityJump event, however it will eventually be with the right click of the book. And the Reference class was unnecessary I'm not sure why I made it.

I just need some help with writing the initial Quest Data to the WorldSaveData and then how I can read and write to it!

Link to comment
Share on other sites

Is there anything wrong with this code:

public class SPNWorldSavedData extends WorldSavedData {
  private static final String DATA_NAME = "QUESTBOOK";

  private NBTTagCompound data = new NBTTagCompound();
  
  // Required constructors
  public SPNWorldSavedData() {
    super(DATA_NAME);
  }
  public SPNWorldSavedData(String s) {
    super(s);
  }
@Override
public void readFromNBT(NBTTagCompound nbt) {
	// TODO Auto-generated method stub

}
@Override
public void writeToNBT(NBTTagCompound nbt) {
	// TODO Auto-generated method stub

}

public static SPNWorldSavedData get(World world) {
	  // The IS_GLOBAL constant is there for clarity, and should be simplified into the right branch.
	  MapStorage storage = world.getMapStorage();
	  SPNWorldSavedData instance = (SPNWorldSavedData) storage.loadData(SPNWorldSavedData.class, DATA_NAME);

	  if (instance == null) {
	    instance = new SPNWorldSavedData();
	    storage.setData(DATA_NAME, instance);
	    instance.getData().setString("obj0description", "DESCRIPTION");
	    instance.markDirty();
	  }
	  return instance;
	}

public NBTTagCompound getData() {
	return data;
}

}

 

And how would I read and write to this data? Would this work:

SPNWorldSavedData data = SPNWorldSavedData.get(world);
data.getData().setString("obj0description", "value");
data.markDirty();

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.