Jump to content

Recommended Posts

Posted

I have a class that contains BlockPos, IBlockState, and world Id.  Is there a way to save this type of data in a NBTTagCompound?

 

className is named myBlockPos

Posted

Just overwrite the method writeToNBT and readFromNBT to save/load your data.

 

For example:

@Override
    public void writeToNBT(NBTTagCompound nbtTagCompound)
    {
        super.writeToNBT(nbtTagCompound);

        nbtTagCompound.setInteger("WorldID", world.getId())
        ........
    }

Posted

Just overwrite the method writeToNBT and readFromNBT to save/load your data.

 

For example:

@Override
    public void writeToNBT(NBTTagCompound nbtTagCompound)
    {
        super.writeToNBT(nbtTagCompound);

        nbtTagCompound.setInteger("WorldID", world.getId())
        ........
    }

I think he has custom class and no writeToNbt and readToNbt methods (and maybe no super methods). And he asks about how he can save "complex" variables like objects to nbt.

Posted

I think you can save byte arrays in nbt. So you could serialize your class and save the byte array. (If byte array dont works I am pretty sure you can save int arrays, so convert byte to int isnt that hard)

Posted

I have a list of blocks and block locations that are suppose to regenerate in specific areas.  I have a command that I can use to add them to a list and once I save and quit, I would like to store this information so that it may be used the next time I load into the world.

Posted

Alright I think I'm getting somewhere.

 

I've noticed that you didn't use the methods nbt.setTag or nbt.getTag inside readFromNBT and writeToNBT.

I also see that you are using a method inside QuestologyWorldData get(World world), called world.setItemData(IDENTIFIER, data)

 

Why not use nbt.setTag/getTag and what is "world.setItemData"

 

Also, is markDirty() saving the information?

 

Thank you!  Sorry for the load of questions!

Posted

When I use my command to add new information, it runs through the write method and adds the data I want it to inside of the nbt and It also works correctly while reading the information.  But, when I restart the game, it doesn't find anything there.  My only guess would be that the world Data information I am using is different every time I load the game.  Here is some code.

 

Event Handler

 

 

@SubscribeEvent

public void onWorldLoad(WorldEvent.Load event){

PacketDispatcher.sendToAll(new SyncWorldData(event.world));

}

 

 

 

PacketDispatcher / sendTo methods

 

 

public class PacketDispatcher

{

// a simple counter will allow us to get rid of 'magic' numbers used during packet registration

private static byte packetId = 0;

private boolean used;

 

/**

* The SimpleNetworkWrapper instance is used both to register and send packets.

* Since I will be adding wrapper methods, this field is private, but you should

* make it public if you plan on using it directly.

*/

private static final SimpleNetworkWrapper dispatcher = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MOD_ID);

 

 

/**

* Call this during pre-init or loading and register all of your packets (messages) here

*/

public static final void registerPackets() {

registerMessage(SyncMiningStats.class);

registerMessage(SyncExcavationStats.class);

registerMessage(SyncWoodCuttingStats.class);

registerMessage(SyncWorldData.class);

 

}

 

/**

* Registers an {@link AbstractMessage} to the appropriate side(s)

*/

private static final <T extends AbstractMessage<T> & IMessageHandler<T, IMessage>> void registerMessage(Class<T> clazz) {

// We can tell by the message class which side to register it on by using #isAssignableFrom (google it)

 

// Also, one can see the convenience of using a static counter 'packetId' to keep

// track of the current index, rather than hard-coding them all, plus it's one less

// parameter to pass.

if (AbstractMessage.AbstractClientMessage.class.isAssignableFrom(clazz)) {

PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.CLIENT);

} else if (AbstractMessage.AbstractServerMessage.class.isAssignableFrom(clazz)) {

PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.SERVER);

} else {

// hopefully you didn't forget to extend the right class, or you will get registered on both sides

PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId, Side.CLIENT);

PacketDispatcher.dispatcher.registerMessage(clazz, clazz, packetId++, Side.SERVER);

}

}

 

//========================================================//

// The following methods are the 'wrapper' methods; again,

// this just makes sending a message slightly more compact

// and is purely a matter of stylistic preference

//========================================================//

 

 

 

/**

* Send this message to the specified player's client-side counterpart.

* See {@link SimpleNetworkWrapper#sendTo(IMessage, EntityPlayerMP)}

*/

public static final void sendTo(IMessage message, EntityPlayerMP player) {

PacketDispatcher.dispatcher.sendTo(message, player);

}

 

/**

* Send this message to everyone.

* See {@link SimpleNetworkWrapper#sendToAll(IMessage)}

*/

public static void sendToAll(IMessage message) {

PacketDispatcher.dispatcher.sendToAll(message);

}

 

/**

* Send this message to everyone within a certain range of a point.

* See {@link SimpleNetworkWrapper#sendToAllAround(IMessage, NetworkRegistry.TargetPoint)}

*/

public static final void sendToAllAround(IMessage message, NetworkRegistry.TargetPoint point) {

PacketDispatcher.dispatcher.sendToAllAround(message, point);

}

 

/**

* Sends a message to everyone within a certain range of the coordinates in the same dimension.

* Shortcut to {@link SimpleNetworkWrapper#sendToAllAround(IMessage, NetworkRegistry.TargetPoint)}

*/

public static final void sendToAllAround(IMessage message, int dimension, double x, double y, double z, double range) {

PacketDispatcher.sendToAllAround(message, new NetworkRegistry.TargetPoint(dimension, x, y, z, range));

}

 

/**

* Sends a message to everyone within a certain range of the player provided.

* Shortcut to {@link SimpleNetworkWrapper#sendToAllAround(IMessage, NetworkRegistry.TargetPoint)}

*/

public static final void sendToAllAround(IMessage message, EntityPlayer player, double range) {

PacketDispatcher.sendToAllAround(message, player.worldObj.provider.getDimensionId(), player.posX, player.posY, player.posZ, range);

}

 

/**

* Send this message to everyone within the supplied dimension.

* See {@link SimpleNetworkWrapper#sendToDimension(IMessage, int)}

*/

public static final void sendToDimension(IMessage message, int dimensionId) {

PacketDispatcher.dispatcher.sendToDimension(message, dimensionId);

}

 

/**

* Send this message to the server.

* See {@link SimpleNetworkWrapper#sendToServer(IMessage)}

*/

public static final void sendToServer(IMessage message) {

PacketDispatcher.dispatcher.sendToServer(message);

}

}

 

 

 

SnycWorldData Class

 

 

public class SyncWorldData extends AbstractClientMessage<SyncWorldData>{

 

private NBTTagCompound world_data;

 

public SyncWorldData(){}

 

public SyncWorldData(World world){

world_data = new NBTTagCompound();

WorldData.get(world).writeToNBT(world_data);

//get method

 

 

public static WorldData get(World world){

WorldData data = (WorldData)world.loadItemData(WorldData.class, Identifier);

if (data == null){

data = new WorldData();

world.setItemData(Identifier, data);

}

return data;

}

 

 

 

//WriteToNBT method

 

 

public void writeToNBT(NBTTagCompound nbt) {

 

NBTTagList tagList = new NBTTagList();

NBTTagCompound tag = new NBTTagCompound();

 

for(Tuple test : currentRespawnLocs.keySet()){

BlockPos pos = test.getPos();

IBlockState state = test.getState();

int blockId = Requirements.getBlockId(state);

int blockMeta = Requirements.getBlockMeta(state);

int worldId = test.getWorldId();

 

                        //STORES INFORMATION IN ARRAY OF INTS

int[] tempPos = new int[]{pos.getX(),pos.getY(),pos.getZ(), blockId, blockMeta, worldId};

posArray.add(tempPos);

}

 

for(int i = 0; i < posArray.size(); i++){

int[] currentMyBlockPos = posArray.get(i);

if(currentMyBlockPos != null && nbt.getIntArray("myBlockPos" + i) != null){

 

tag.setIntArray("myBlockPos" + i, currentMyBlockPos);

                                //APPEND IF TagList HAS NO LENGTH YET

if(tagList.hasNoTags())

tagList.appendTag(tag);

else

tagList.set(0, tag);

}

}

//STORE TagList INSIDE NBT

nbt.setTag(Identifier, tagList);

}

 

 

 

 

}

 

protected void read(PacketBuffer buffer) throws IOException{

world_data = buffer.readNBTTagCompoundFromBuffer();

}

 

protected void write(PacketBuffer buffer) throws IOException{

buffer.writeNBTTagCompoundToBuffer(world_data);

}

 

public void process(EntityPlayer player, Side side){

WorldData.get(player.worldObj).readFromNBT(world_data);

//READ

 

 

/**

This undoes what the write method does.  It takes all the integers and converts them to blockPos, an IBlockState, and worldId.  It then adds them to the hashMap currentRespawnLocs(a public class variable.

*/

public void readFromNBT(NBTTagCompound nbt) {

NBTTagList tagList = nbt.getTagList(Identifier, Constants.NBT.TAG_COMPOUND);

for(int i = 0; tagList.getCompoundTagAt(0).hasKey("myBlockPos" + i); i++){

int[] currentMyBlockPos = tagList.getCompoundTagAt(0).getIntArray("myBlockPos" + i);

BlockPos pos = new BlockPos(currentMyBlockPos[0], currentMyBlockPos[1], currentMyBlockPos[2]);

int blockId = currentMyBlockPos[3];

int blockMeta = currentMyBlockPos[4];

int worldId = currentMyBlockPos[5];

 

Block tempBlock = Block.getBlockById(blockId);

IBlockState state = tempBlock.getStateFromMeta(blockMeta);

 

currentRespawnLocs.put(new Tuple(this.pos, this.state, this.worldId), true);

}

}

 

 

}

 

}

 

 

 

I know that this is a lot of information.  I don't expect you, nor anyone, to read all of it.  I am just curious if you can spot any errors.  The packetDispatcher should be just fine, as I have used it to store information with players.

Posted

Your code is hard to follow, please post the classes on pastebin, with syntax highlighting. And as separate things, not intertwined and with spoilers like you did.

 

Will do:p

 

Also... why are you using NBT to send your data? :o

 

You tell me.  haha!

 

I haven't used Pastebin before.  Is this how I share it?

http://pastebin.com/u/Atijaf

Posted

There was one more class that I forgot to include in pastebin.

Also, every time i start minecraft and debug.  It goes through the method, WorldData get(World world), and data is null so it creates a new one.  Is that suppose to happen?

I also never call the method, setDirty();

 

I went ahead and changed the static variable to a private class variable.  No change...  I'll leave it a private class variable though

 

Here's my SyncWorldData class

http://pastebin.com/dgNBV6G2

Posted

I've messed with the code a little bit and created a method called saveData.

I call this method after I add data to nbt.

 

 

 

public void saveData(World world){

WorldData data = (WorldData) world.loadItemData(WorldData.class, Identifier);

world.setItemData(Identifier, data);

data.markDirty();

world.getPerWorldStorage().saveAllData();

}

 

 

 

I run that bit of code, save and quit, reload, and data is null again.  The only thing that I can think of that would be wrong is the retrieval of the information..  Or maybe even saving it.  Is there anything wrong with this bit of code.

 

I am coding in 1.8, if that makes a difference.

 

Posted

I apologize!  Haha.  How long should I allow it to sit until it saves?  I used markDirty and waited about 5 minutes to see if it had changed, but it was still markedDirty when I checked it.

Posted

It will save when you either exist the world or open the ingame menu. In multiplayer it will save at the intervals set in the config.

 

Thanks for explaining that.  That's what I was worried about...

I am thinking that I probably missed something obvious near the beginning.

After I use markDirty(), it sets dirty to true.  I then save it. Re enter the command to add more data and before it even gets to the mothod, markDirty(), dirty is still true. (I'm gussing dirty is suppose to be switched back to false when it saves)

 

I will look at my code for a while and possibly make a new thread that deals more with WorldSavedData.

Posted

All I can figure out is that the method "readFromNBT(NBTTagCompound nbt) is not getting called automatically, but when I manually call it, nbt has nothing saved in it.

 

Am I suppose to register WorldData?  or just use the method "public static WorldData get(World world).

 

Also, when the command to set a few variables is used and I use the method "nbt.setTag(Identifier, tagList)"  nbt gets data, and then closes.  If I call the method again, nbt has no data again, as if I never used the command once.  It's weird...

Posted

You probably want to make all variables statis, make setters for them and in the setters call markDirty().

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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

    • It is 1.12.2 - I have no idea if there is a 1.12 pack
    • Okay, but does the modpack works with 1.12 or just with 1.12.2, because I need the Forge client specifically for Minecraft 1.12, not 1.12.2
    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
    • Do you use features of inventory profiles next (ipnext) or is there a change without it?
  • Topics

×
×
  • Create New...

Important Information

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