Jump to content

1.19.2 Golems not saving their persistant data after spawning from a packet[SOLVED]


ChasePlays

Recommended Posts

I am created a packet that spawns 3 golems and adds a timer to their persistant data along with the name of the owner who spawned them.

public boolean handle(Supplier<NetworkEvent.Context> supplier){
        NetworkEvent.Context context = supplier.get();
        context.enqueueWork(()-> {
            Player p = context.getSender();
            if(p.getPersistentData().contains(XprtData.SALUS_CLASS)){
                if(!p.getPersistentData().contains(XprtData.SALUS_DEFENDER_TIMER)){
                    for(int i=0;i<3;i++){
                        IronGolem golem = (IronGolem) EntityType.IRON_GOLEM.spawn(p.createCommandSourceStack().getLevel(), null, null,
                                p.blockPosition(), MobSpawnType.MOB_SUMMONED, true, false);

                        golem.getPersistentData().putInt(XprtData.SALUS_DEFENDER_TIMER, Utils.Seconds(50));
                        golem.getPersistentData().putString(XprtData.SALUS_DEFENDER_OWNER, p.getScoreboardName());
                        golem.setCustomName(Component.literal(ChatFormatting.GOLD + "" + p.getScoreboardName() + "'s Defender"));
                    }

                    p.getPersistentData().putInt(XprtData.SALUS_DEFENDER_TIMER, Utils.Seconds(5));
                } else {
                    int time = p.getPersistentData().getInt(XprtData.SALUS_DEFENDER_TIMER);
                    Utils.Message(p, ChatFormatting.RED + "You cannot summon defenders right now, " + Utils.TicksToSeconds(time) + " seconds remaining.");
                }
            }
        });

        return true;
    }

the problem is when I call upon their data in an external class here

@SubscribeEvent
    public static void onLivingTick(LivingEvent.LivingTickEvent e){
        LivingEntity entity = e.getEntity();
        if(entity instanceof IronGolem){
            if(entity.getPersistentData().contains(XprtData.SALUS_DEFENDER_TIMER)){
                Mob mob = (Mob)entity;
                for(LivingEntity target : Utils.getNear(entity, 8)){
                    if(target instanceof Player || target instanceof Villager || target instanceof Wolf || target instanceof IronGolem){
                        mob.setTarget(null);
                    } else {
                        mob.setTarget(target);
                    }
                }
            }
            if(entity.getPersistentData().contains(XprtData.SALUS_DEFENDER_OWNER)){
                String owner = entity.getPersistentData().getString(XprtData.SALUS_DEFENDER_OWNER);
                for(String pNames : entity.getServer().getPlayerNames()){
                    if(pNames.equals(owner)){
                        Player p = entity.getServer().getPlayerList().getPlayerByName(owner);
                        if(entity.distanceTo(p) >= 7f){
                            entity.teleportTo(p.getBlockX(), p.getBlockY()+1, p.getBlockZ());
                        }
                    }
                }
            }
        }
    }

neither XprtData.SALUS_DEFENDER_TIMER nor XprtData.SALUS_DEFENDER_OWNER contain any data, for the timer it starts off at 0 when it should be (22*50) and defender owner should contain the players name which instead contains a blank.

 

This isnt my first time doing something like this, ive done it will illagers and it worked perfectly but when it comes to golems it doesnt work at all. I could do with it the timer killing them at -(22*50) but it wont help that i still need the owners name to teleport them to the owner if they move to far away. Any Ideas? Im calling this packet from a keybind by the way

 

EDIT: 

I figured out the issue I accidentally had the same String name for both of the Strings so it kept overriding eachother

Edited by ChasePlays
Link to comment
Share on other sites

  • ChasePlays changed the title to 1.19.2 Golems not saving their persistant data after spawning from a packet[SOLVED]

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



×
×
  • Create New...

Important Information

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