Jump to content

Recommended Posts

Posted (edited)

Hi, I am trying to make a custom horse.

The NBT does not save. The marking data is completely different.

First attatchmet was at spawn, second was after a reload, third was after another.

 

EntityModHorse:

package mods.giantnuker.horse.entity;


import javax.annotation.Nullable;

import com.google.common.base.Predicate;

import mods.giantnuker.horse.Markings;
import mods.giantnuker.horse.ai.EntityAIHorseAttack;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.ai.EntityAIFollowParent;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIMate;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWanderAvoidWater;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.passive.AbstractChestHorse;
import net.minecraft.entity.passive.AbstractHorse;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;
import net.minecraft.world.storage.loot.LootTableList;

public class EntityModHorse extends AbstractHorse {
	public int openMouthCounter = 0;
	public Markings markings = new Markings();
	public Predicate<Entity> BREEDABLE_HORSE = new Predicate<Entity>() {
		@Override
		public boolean apply(Entity input) {
			if (!(input instanceof EntityModHorse))return false;
			EntityModHorse horse = (EntityModHorse) input;
			if (!horse.canBreed(EntityModHorse.this)) return false;
			return true;
		}
		
	};
	public EntityModHorse(World world) {
		super(world);
		this.canGallop = true;
	}
	@Override
	protected void initEntityAI() {
		this.tasks.addTask(0, new EntityAISwimming(this));
		this.tasks.addTask(1, new EntityAIHorseAttack(this));
		this.tasks.addTask(2, new EntityAIMate(this, 1.0D, EntityModHorse.class));
		this.tasks.addTask(4, new EntityAIFollowParent(this, 1.3D));
		this.tasks.addTask(6, new EntityAIWanderAvoidWater(this, 0.8D));
		this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
		this.tasks.addTask(8, new EntityAILookIdle(this));
		this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true, new Class[0]));
	}
	public boolean isBreedingItem(ItemStack item) {
		return item.getItem() == Items.GOLDEN_CARROT || item.getItem() == Items.SPECKLED_MELON || item.getItem() == Items.GOLDEN_APPLE;
	}
	@Override
	protected AbstractHorse getClosestHorse(Entity entityIn, double range) {
		return closestBreedableHorse(range);
	}
	@Nullable
	public EntityModHorse closestBreedableHorse(double range) {
		EntityModHorse cHorse = null;
		double dist = Double.MAX_VALUE;
		for (Entity entity : this.world.getEntitiesInAABBexcluding(this, this.getEntityBoundingBox().expand(range, range, range), BREEDABLE_HORSE)) {
			if (this.getDistanceSqToEntity(entity) < dist) {
				cHorse = (EntityModHorse) entity;
				dist = this.getDistanceSqToEntity(entity);
			}
		}
		return cHorse;
	}
	
	public boolean canBreed(EntityModHorse breedWith) {
		return true;
	}
	@Override
	public void onUpdate() {
		super.onUpdate();
		if (this.openMouthCounter > 0) {
			this.openMouthCounter--;
			this.setHorseWatchableBoolean(64, true);
		} else if (openMouthCounter == -1) {
			this.setHorseWatchableBoolean(64, true);
		} //else {
			//this.setHorseWatchableBoolean(64, false);
		//}
	}
	/**open mouth for time, time may be -1 for until set to close*/
	public void openHorseMouth(int time) {
		if (!this.world.isRemote) {
			this.openMouthCounter = time;
			this.setHorseWatchableBoolean(64, true);
		}
	}
	
	public void closeHorseMouth() {
		if (!this.world.isRemote) {
			this.openMouthCounter = 0;
			this.setHorseWatchableBoolean(64, false);
		}
	}
	@Override
	public EntityModHorse createChild(EntityAgeable mate) {
		EntityModHorse horse = new EntityModHorse(this.world);
		horse.markings = Markings.merge(this.markings, ((EntityModHorse)mate).markings);
		return horse;
	}
	
	public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
        ItemStack itemstack = player.getHeldItem(hand);
        boolean flag = !itemstack.isEmpty();

        if (flag && itemstack.getItem() == Items.SPAWN_EGG)
        {
            return super.processInteract(player, hand);
        }
        else
        {
            if (!this.isChild())
            {
                if (this.isTame() && player.isSneaking())
                {
                    this.openGUI(player);
                    return true;
                }

                if (this.isBeingRidden())
                {
                    return super.processInteract(player, hand);
                }
            }

            if (flag)
            {
                if (this.handleEating(player, itemstack))
                {
                    if (!player.capabilities.isCreativeMode)
                    {
                        itemstack.shrink(1);
                    }

                    return true;
                }

                if (itemstack.interactWithEntity(player, this, hand))
                {
                    return true;
                }

                if (!this.isTame())
                {
                    this.makeMad();
                    return true;
                }

                //boolean flag1 = HorseArmorType.getByItemStack(itemstack) != HorseArmorType.NONE;
                boolean flag2 = !this.isChild() && !this.isHorseSaddled() && itemstack.getItem() == Items.SADDLE;

                if (/*flag1 ||*/ flag2)
                {
                    this.openGUI(player);
                    return true;
                }
            }

            if (this.isChild())
            {
                return super.processInteract(player, hand);
            }
            else
            {
                this.mountTo(player);
                return true;
            }
        }
    }
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
		super.writeToNBT(nbt);
		nbt.setTag("looks", markings.serializeNBT());
		return nbt;
	}
	@Override
	public void readFromNBT(NBTTagCompound nbt) {
		super.readFromNBT(nbt);
		markings.deserializeNBT(nbt.getTag("looks"));
	}
	
	protected SoundEvent getAmbientSound()
    {
        super.getAmbientSound();
        return SoundEvents.ENTITY_HORSE_AMBIENT;
    }

    protected SoundEvent getDeathSound()
    {
        super.getDeathSound();
        return SoundEvents.ENTITY_HORSE_DEATH;
    }

    protected SoundEvent getHurtSound(DamageSource p_184601_1_)
    {
        super.getHurtSound(p_184601_1_);
        return SoundEvents.ENTITY_HORSE_HURT;
    }

    protected SoundEvent getAngrySound()
    {
        super.getAngrySound();
        return SoundEvents.ENTITY_HORSE_ANGRY;
    }

    protected ResourceLocation getLootTable()
    {
        return LootTableList.ENTITIES_HORSE;
    }
    
    public static class GroupData implements IEntityLivingData
    {
        public Markings variant;

        public GroupData(Markings variantIn)
        {
            this.variant = variantIn;
        }
    }

}

Markings

package mods.giantnuker.horse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;

import mods.giantnuker.backslash.DoubleMap;
import mods.giantnuker.backslash.ListMap;
import mods.giantnuker.horse.entity.EntityModHorse;
import mods.giantnuker.javautil.ArrayUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.LayeredTexture;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.INBTSerializable;

public class Markings implements INBTSerializable {
	public static final String KEY_BODY_PART = "body_part";
	public static final String KEY_BODY = "body";
	public static final String KEY_MARKING = "marking";
	public static final String KEY_ADDED_ALL = "add_all";
	//Registry
	public static Random rand = new Random();
	
	public static Markings merge(Markings marksA, Markings marksB) {
		if (marksB == null || marksA.equals(marksB)) return marksA.copy();
		Markings markings = new Markings();
		markings.markings.clear();
		/*if (rand.nextInt(5) != 0)*/ markings.bodyBase = rand.nextBoolean() ? marksA.bodyBase : marksB.bodyBase;
		for (Entry<String, List<String>> e : marksA.markings.entrySet()) {
			for (String s : e.getValue()) {
				if (rand.nextBoolean()) markings.markings.add(e.getKey(), s);
			}
		}
		for (Entry<String, List<String>> e : marksB.markings.entrySet()) {
			for (String s : e.getValue()) {
				if (rand.nextBoolean()) markings.markings.add(e.getKey(), s);
			}
		}
		return markings;
	}
	public static final HashMap<String, ResourceLocation> renders = new HashMap();
	public static final DoubleMap<String, String, ResourceLocation> tex_ids = new DoubleMap();
	static {
		//Body
		tex_ids.put(KEY_BODY, "black", new ResourceLocation(HorseMod.MODID, "textures/entity/body/black.png"));
		tex_ids.put(KEY_BODY, "white", new ResourceLocation(HorseMod.MODID, "textures/entity/body/white.png"));
		tex_ids.put(KEY_BODY, "creamy", new ResourceLocation(HorseMod.MODID, "textures/entity/body/creamy.png"));
		tex_ids.put(KEY_BODY, "chestnut", new ResourceLocation(HorseMod.MODID, "textures/entity/body/chestnut.png"));
		tex_ids.put(KEY_BODY, "brown", new ResourceLocation(HorseMod.MODID, "textures/entity/body/brown.png"));
		tex_ids.put(KEY_BODY, "darkbrown", new ResourceLocation(HorseMod.MODID, "textures/entity/body/darkbrown.png"));
		tex_ids.put(KEY_BODY, "gray", new ResourceLocation(HorseMod.MODID, "textures/entity/body/gray.png"));
		//Mane
		tex_ids.put(KEY_BODY_PART, "mane_black0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/black0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_black1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/black1.png"));
		tex_ids.put(KEY_BODY_PART, "mane_white0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/white0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_white1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/white1.png"));
		tex_ids.put(KEY_BODY_PART, "mane_creamy0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/creamy0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_creamy1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/creamy1.png"));
		tex_ids.put(KEY_BODY_PART, "mane_chestnut0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/chestnut0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_chestnut1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/chestnut1.png"));
		tex_ids.put(KEY_BODY_PART, "mane_brown0", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/brown0.png"));
		tex_ids.put(KEY_BODY_PART, "mane_brown1", new ResourceLocation(HorseMod.MODID, "textures/entity/mane/brown1.png"));
		//Tail
		tex_ids.put(KEY_BODY_PART, "tail_white0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/white0.png"));
		tex_ids.put(KEY_BODY_PART, "tail_white1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/white1.png"));
		tex_ids.put(KEY_BODY_PART, "tail_black0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/black0.png"));
		tex_ids.put(KEY_BODY_PART, "tail_black1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/black1.png"));
		tex_ids.put(KEY_BODY_PART, "tail_creamy_top0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/creamy_top0.png"));
		tex_ids.put(KEY_BODY_PART, "tail_creamy_top1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/creamy_top1.png"));
		tex_ids.put(KEY_BODY_PART, "tail_chestnut0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/chestnut0.png"));
		tex_ids.put(KEY_BODY_PART, "tail_chestnut1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/chestnut1.png"));
		tex_ids.put(KEY_BODY_PART, "tail_chestnut_top1", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/chestnut3.png"));
		tex_ids.put(KEY_BODY_PART, "tail_chestnut_top0", new ResourceLocation(HorseMod.MODID, "textures/entity/tail/chestnut2.png"));
		//add to all
		tex_ids.put(KEY_ADDED_ALL, "chest", new ResourceLocation(HorseMod.MODID, "textures/entity/misc/chest.png"));
		tex_ids.put(KEY_ADDED_ALL, "riding_gear", new ResourceLocation(HorseMod.MODID, "textures/entity/misc/riding_gear.png"));
		//Additional Markings
		tex_ids.put(KEY_MARKING, "blackdots", new ResourceLocation(HorseMod.MODID, "textures/entity/marking/blackdots.png"));
		tex_ids.put(KEY_MARKING, "greyfield", new ResourceLocation(HorseMod.MODID, "textures/entity/marking/greyfield.png"));
		tex_ids.put(KEY_MARKING, "blackfield", new ResourceLocation(HorseMod.MODID, "textures/entity/marking/blackfield.png"));
		tex_ids.put(KEY_MARKING, "blaze", new ResourceLocation(HorseMod.MODID, "textures/entity/marking/blaze.png"));
		tex_ids.put(KEY_MARKING, "white", new ResourceLocation("textures/entity/horse/horse_markings_white.png"));
		tex_ids.put(KEY_MARKING, "whitefield", new ResourceLocation("textures/entity/horse/horse_markings_whitefield.png"));
		tex_ids.put(KEY_MARKING, "whitedots", new ResourceLocation("textures/entity/horse/horse_markings_whitedots.png"));
		tex_ids.put(KEY_MARKING, "blackshag", new ResourceLocation("textures/entity/horse/horse_markings_blackdots.png"));
	}
	//Instance
	public Markings() {
		randomize();
	}
	public Markings(int oldID) {
		/*int j = (oldID & 255) % 7;
		int k = ((oldID & 65280) >> 8) % 5;
		switch (j) {
		case 1:	baseCoat = "creamy";
		case 2:	baseCoat = "chestnut";
		case 3:	baseCoat = "brown";
		case 4:	baseCoat = "black";
		case 5:	baseCoat = "gray";
		case 6:	baseCoat = "darkbrown";
		default:baseCoat = "white";
		}
		switch (k) {
		case 1: markings.add("marks_whitefield");
		case 2: markings.add("marks_whitedots");
		case 3: markings.add("marks_blackdots");
		default:markings.add("marks_white");
		}*/
	}
	public void randomize() {
		markings.clear();
		bodyBase = tex_ids.keySet(KEY_BODY).get(new Random().nextInt(tex_ids.size(KEY_BODY)));
		int bodyParts = rand.nextInt(5);
		for (int i = 0; i < bodyParts; i++) {
			String mark = tex_ids.keySet(KEY_BODY_PART).get(rand.nextInt(tex_ids.size(KEY_BODY_PART)));
			if (markings.contains(KEY_BODY_PART, mark)) continue;
			markings.add(KEY_BODY_PART, mark);
		}
		int marks = rand.nextInt(5);
		for (int i = 0; i < marks; i++) {
			String mark = tex_ids.keySet(KEY_MARKING).get(rand.nextInt(tex_ids.size(KEY_MARKING)));
			if (markings.contains(KEY_MARKING, mark)) continue;
			markings.add(KEY_MARKING, mark);
		}
	}
	public ListMap<String, String> markings = new ListMap();
	public String bodyBase = null;
	@Override
	public void deserializeNBT(NBTBase arg0) {
		if (!(arg0 instanceof NBTTagCompound)) {
			return;
		}
		NBTTagCompound nbt = (NBTTagCompound) arg0;
		System.out.println("-------------------------");
		for (String key : nbt.getKeySet()) {
			if (key == "bodyBase") bodyBase = nbt.getString(key);
			else if (nbt.getTag(key) instanceof NBTTagList){
				markings.clear(key);
				NBTTagList list = (NBTTagList) nbt.getTag(key);
				for (NBTBase nbt2 : list) {
					if (nbt2 instanceof NBTTagString) markings.add(key, ((NBTTagString)nbt2).getString());
				}
			}
		}
		System.out.println(markings);
	}
	@Override
	public NBTBase serializeNBT() {
		NBTTagCompound nbt = new NBTTagCompound();
		nbt.setString("bodyBase", bodyBase);
		for (Entry<String, List<String>> e : markings.entrySet()) {
			NBTTagList tl = new NBTTagList();
			for (String s : e.getValue()) tl.appendTag(new NBTTagString(s));
			nbt.setTag(e.getKey(), tl);
		}
		return nbt;
	}
	public ResourceLocation getTex() {
		String texId = "gianthorses:b" + bodyBase;
		for (Entry<String, List<String>> list : markings.entrySet()) {
			texId +=  "[" + list.getKey() + "]";
			for (String s : list.getValue()) texId += "(" + s + ")";
		}
		ResourceLocation tex = renders.get(texId);
		if (tex == null) {
			tex = new ResourceLocation(texId);
			ArrayList<String> tex_mp = new ArrayList();
			tex_mp.add(tex_ids.get(KEY_BODY, bodyBase).toString());
			//for (int i = 0; i < markings.size(); i++) tex_lst[i + 1] = (tex_ids.get(markings.get(i)).toString());
			for (Entry<String, List<String>> list: markings.entrySet()) {
				for (String s : list.getValue()) tex_mp.add(tex_ids.get(list.getKey(), s).toString());
			}
			for (String s : tex_ids.keySet(KEY_ADDED_ALL)) {
				tex_mp.add(tex_ids.get(KEY_ADDED_ALL, s).toString());
			}
			Minecraft.getMinecraft().getTextureManager().loadTexture(tex, new LayeredTexture(tex_mp.toArray(new String[0])));
			renders.put(texId, tex);
		}
		return tex;
	}
	public Markings copy() {
		System.out.println("cpy");
		Markings m = new Markings();
		m.markings.clear();
		m.bodyBase = this.bodyBase;
		for (Entry<String, List<String>> e : this.markings.entrySet()) {
			for (String s : e.getValue()) {
				m.markings.add(e.getKey(), s);
			}
		}
		System.out.println("B4:" + markings);
		System.out.println("AFTER:" + m.markings);
		return m;
	}
}

All my checks show that the horses have the right data, do I need to send packets to the client to tell it what the horse's NBT is?

2017-10-16_18.27.36.png

2017-10-16_18.27.58.png

2017-10-16_18.29.30.png

Edited by GiantNuker
Posted
10 hours ago, diesieben07 said:

This has nothing to do with NBT, but yes, you do need to tell the client what the texture should be.

You can either use a DataParameter or custom packets.

Thanks, Iwas wondering why horses used a data peramiter for that :)

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

×
×
  • Create New...

Important Information

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