Jump to content

Recommended Posts

Posted

I have a block with a custom amount of "power" in it, and I want the block to get brighter as there is more power.  I have three textures I want to use for different power ranges, but when the block gets into a new power range, it dosen't update.  If I place a block next to it, or force it to update, it works, but it won't work on it's own.  I think it might have something to do with textures being client-side and tile entities being server-side, but I dont know how to fix it.  Thanks in advance for any help.

 

Block:

 

 

package Technomage3.both.Blocks;

import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import am2.AMCore;
import am2.blocks.BlocksCommonProxy;
import am2.particles.AMParticle;
import am2.particles.ParticleFloatUpward;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChatMessageComponent;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import Technomage3.Core.Blocks.TN3Block;
import Technomage3.Core.Blocks.TN3BlockContainer;
import Technomage3.Core.Utils.TN3Utils;
import Technomage3.both.Blocks.tileentities.TileEntityMagicRune;
import Technomage3.both.Power.IUsePower;

public class BlockMagicRune extends TN3BlockContainer {

private Icon[] icons = new Icon[3];

public BlockMagicRune(String realName, String unlocalizedName, CreativeTabs tab) {
	super(realName, unlocalizedName, tab);
	this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.01F, 1.0F);
	this.setLightOpacity(0);
	this.setTickRandomly(true);
}

public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
{
	return AxisAlignedBB.getAABBPool().getAABB((double)par2 + super.minX, (double)par3 + super.minY, (double)par4 + super.minZ, (double)par2 + super.maxX, (double)par3 + super.maxY, (double)par4 + super.maxZ);
}

public void setBlockBoundsForItemRender()
{
	this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.01F, 1.0F);
}


public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int meta, float hitX, float hitY, float hitZ)
{

	TileEntityMagicRune tile = (TileEntityMagicRune) world.getBlockTileEntity(x, y, z);

	if(!world.isRemote){
		player.sendChatToPlayer(ChatMessageComponent.createFromTranslationKey(
				"Power Level: " + tile.getPowerLevel() + 
				", Power: " + tile.getPower()
				));
	}

	return true;
}

@Override
public TileEntity createNewTileEntity(World world) {
	return new TileEntityMagicRune();
}

public void randomDisplayTick(World world, int x, int y, int z, Random rand)
{
	if(world.isRemote && this.getPowerLevel(world, x, y, z) >= 50){

	} else if(world.isRemote && this.getPowerLevel(world, x, y, z) >= 0){


	} else{



	}
}

public int getPowerLevel(IBlockAccess world, int x, int y, int z) {

	TileEntityMagicRune tile = (TileEntityMagicRune) world.getBlockTileEntity(x, y, z);

	return tile.getPowerLevel();
}


public Icon getBlockTexture(IBlockAccess world, int x, int y, int z, int par5)
{

	if(this.getPowerLevel(world, x, y, z) > 30){

		return icons[2];

	} else if(this.getPowerLevel(world, x, y, z) > 0){

		return icons[1];

	} else{

		return icons[0];

	}

}

public void registerIcons(IconRegister register)
{

	icons[2] = register.registerIcon(TN3Utils.getTexturePrefix() + "magicRuneHigh");

	icons[1] = register.registerIcon(TN3Utils.getTexturePrefix() + "magicRuneMid");

	icons[0] = register.registerIcon(TN3Utils.getTexturePrefix() + "magicRuneLow");

}

public Icon getIcon(int par1, int par2)
{
	return icons[1];
}

public int tickRate(World par1World)
{
	return 1;
}

public void onBlockAdded(World world, int x, int y, int z) {

	TileEntityMagicRune rune = (TileEntityMagicRune) world.getBlockTileEntity(x, y, z);

	rune.onCreated();

}

public boolean renderAsNormalBlock()
{
	return false;
}

public boolean isOpaqueCube()
{
	return false;
}

public static boolean isNormalCube(int par0)
    {
        return false;
    }
public boolean isBlockSolid(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
    {
        return false;
    }

public boolean getTickRandomly()
    {
        return true;
    }

public void updateTick(World world, int x, int y, int z, Random rand)
{

	world.scheduleBlockUpdate(x, y, z, super.blockID, this.tickRate(world));

}

}

 

 

 

TileEntity:

 

 

package Technomage3.both.Blocks.tileentities;

import java.util.ArrayList;
import java.util.Stack;

import Technomage3.both.Blocks.TMBlocks;
import Technomage3.both.Power.IRecivePower;
import Technomage3.both.Power.IUsePower;
import Technomage3.both.Power.PowerHelper;
import Technomage3.both.Power.PowerPacket;
import Technomage3.both.Power.TileEntityMagicConduit;
import Technomage3.both.Power.TileEntityMagicMachine;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;

public class TileEntityMagicRune extends TileEntity {

private int power = 0;

public static final int ratio = 50; 

public void updateEntity() {

	worldObj.scheduleBlockUpdate(xCoord, yCoord, zCoord, TMBlocks.magicRune.blockID, 1);

}

public static class MagicRunePowerHandeler implements IRecivePower {

	private World world;

	private ArrayList<TileEntityMagicRune> runes;

	private PowerPacket power;

	public MagicRunePowerHandeler(TileEntityMagicRune tile) {

		world = tile.worldObj;

		runes = findAll(tile);

		power = new PowerPacket();

		setPower(new PowerPacket(getMaxPower(runes)));

	}

	private int getMaxPower(ArrayList<TileEntityMagicRune> runes2) {

		int max = 0;

		for(TileEntityMagicRune r : runes2)
			if(r.getPower() > max)
				max = r.getPower();

		return max;
	}

	public static ArrayList<TileEntityMagicRune> findAll(TileEntityMagicRune rune){

		ArrayList<TileEntityMagicRune> runes = new ArrayList<TileEntityMagicRune>();

		Stack<TileEntityMagicRune> qeue = new Stack<TileEntityMagicRune>();

		qeue.add(rune);

		while(!qeue.isEmpty()){

			TileEntityMagicRune r = qeue.pop();

			if(!runes.contains(r))
				qeue.addAll(getConnected(r));

			if(!runes.contains(r))
				runes.add(r);

		}

		return runes;
	}

	private static ArrayList<TileEntityMagicRune> getConnected(TileEntityMagicRune rune){

		ArrayList<TileEntityMagicRune> runes = new ArrayList<TileEntityMagicRune>();

		TileEntity tile = rune.worldObj.getBlockTileEntity(rune.xCoord + 1, rune.yCoord, rune.zCoord);

		if(tile != null && tile instanceof TileEntityMagicRune)
			runes.add((TileEntityMagicRune) tile);

		tile = rune.worldObj.getBlockTileEntity(rune.xCoord, rune.yCoord, rune.zCoord + 1);

		if(tile != null && tile instanceof TileEntityMagicRune)
			runes.add((TileEntityMagicRune) tile);

		tile = rune.worldObj.getBlockTileEntity(rune.xCoord - 1, rune.yCoord, rune.zCoord);

		if(tile != null && tile instanceof TileEntityMagicRune)
			runes.add((TileEntityMagicRune) tile);

		tile = rune.worldObj.getBlockTileEntity(rune.xCoord, rune.yCoord, rune.zCoord - 1);

		if(tile != null && tile instanceof TileEntityMagicRune)
			runes.add((TileEntityMagicRune) tile);

		return runes;
	}

	@Override
	public PowerPacket getPower() {
		return power;
	}

	@Override
	public void setPower(PowerPacket newPower) {
		power = newPower;

		for(TileEntityMagicRune r : runes)
			r.setPower(power.getAmount());
	}

	@Override
	public int getMaxPower() {
		return 50 * ratio;
	}

	public boolean equals(Object o){

		if(!(o instanceof MagicRunePowerHandeler) || o == null)
			return false;

		MagicRunePowerHandeler m = (MagicRunePowerHandeler) o;

		return m.runes.containsAll(runes) && runes.containsAll(m.runes);

	}

	@Override
	public boolean recievePower(PowerPacket newPower) {
		boolean b = power.merge(newPower);

		power.setToMaxIfNessecary(getMaxPower());

		for(TileEntityMagicRune r : runes)
			r.setPower(power.getAmount());

		return b;
	}

	public ArrayList<TileEntityMagicRune> getRunes() {
		return runes;
	}




}

@Override
	public boolean equals(Object o){

		if(!(o instanceof TileEntity))
			return false;

		TileEntity t = (TileEntity) o;

		return t.xCoord == xCoord && t.yCoord == yCoord && t.zCoord == zCoord;

	}

public void setPowerLevel(int i) {
	power = i * ratio;

}

public int getPowerLevel() {
	return (int) (power / ratio);
}

/**
 * Reads a tile entity from NBT.
 */
public void readFromNBT(NBTTagCompound nbt)
{
    super.readFromNBT(nbt);
    power = nbt.getInteger("Power");
}

/**
 * Writes a tile entity to NBT.
 */
public void writeToNBT(NBTTagCompound nbt)
{
    
	super.writeToNBT(nbt);
	nbt.setInteger("Power", power);

}

public void damage(int damage){
	power -= damage * ratio;
}

public boolean canPass(){
	return power <= 0;
}

public boolean isAtFullPower() {
	return getPowerLevel() >= 50;
}

public void setPower(int i) {
	power = i;

}

public int getPower() {
	return power;
}

public void onCreated() {

	for(ForgeDirection f : new ForgeDirection[] {ForgeDirection.NORTH, ForgeDirection.EAST, ForgeDirection.SOUTH, ForgeDirection.WEST}){

		TileEntity t = worldObj.getBlockTileEntity(xCoord + f.offsetX, yCoord, zCoord + f.offsetZ);

		if(t != null && t instanceof TileEntityMagicRune){
			TileEntityMagicRune r = (TileEntityMagicRune) t;

			if(r.getPower() >= this.getPower())
				power = r.getPower();
			else
				r.setPower(power);

		}

	}


}

}

 

 

Posted

Hi

 

Block textures only update when there is a change.  When rendering Blocks, minecraft uses a 'cached renderlist'  that is only refreshed when the block changes.  So if you try to animate your Block using the Block rendering methods above, you won't see anything until the Block is changed or updated.

If you want your Block to be animated without updating it, you need to either use an animated texture for your Block, or use a TileEntity with TileEntitySpecialRender.

 

For more info see here

http://greyminecraftcoder.blogspot.com.au/2013/07/rendering-world-more-details-including.html

 

-TGG

Posted

Actually all you have to do is update the block when you want the texture to change using

worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);

Just make sure you are also synchronising the client tile data with the server using

@Override
public Packet getDescriptionPacket() {
NBTTagCompound tagCompound = new NBTTagCompound();
this.writeToNBT(tagCompound);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, tagCompound);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
readFromNBT(pkt.func_148857_g());
}

I am the author of Draconic Evolution

Posted

I have the renderer, but the texture flickers, and sometimes disapears.

 

Here's my code:

Renderer:

 

 

package Technomage3.both.Renderers;

import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import Technomage3.both.Blocks.tileentities.TileEntityMagicRune;
import Technomage3.both.Models.ModelMagicRune;

public class MagicRuneRenderer extends TileEntitySpecialRenderer {

@Override
public void renderTileEntityAt(TileEntity tileentity, double d0, double d1,
		double d2, float f) {
	renderRune((TileEntityMagicRune) tileentity, d0, d1, d2, f);

}

private void renderRune(TileEntityMagicRune tile, double x,
		double y, double z, float f) {

	Tessellator tessellator = Tessellator.instance;
    
	this.bindTexture(getTexture(tile.getPowerLevel()));
    GL11.glPushMatrix();
    GL11.glTranslated(x, y, z);
    tessellator.startDrawingQuads();
    
    tessellator.addVertexWithUV(0, 0, 0, 0, 0);
    tessellator.addVertexWithUV(0, 0, 1, 0, 1);
    tessellator.addVertexWithUV(1, 0, 1, 1, 1);
    tessellator.addVertexWithUV(1, 0, 0, 1, 0);

    tessellator.addVertexWithUV(0, 0, 0, 0, 0);
    tessellator.addVertexWithUV(1, 0, 0, 1, 0);
    tessellator.addVertexWithUV(1, 0, 1, 1, 1);
    tessellator.addVertexWithUV(0, 0, 1, 0, 1);
    
    tessellator.draw();
    GL11.glPopMatrix();

}

private ResourceLocation getTexture(int power) {

	if(power > 30){

		return icons[2];

	} else if(power > 0){

		return icons[1];

	} else{

		return icons[0];

	}

}

private ResourceLocation[] icons = new ResourceLocation[]{

		new ResourceLocation("tn3", "textures/blocks/magicRuneLow.png"),
		new ResourceLocation("tn3", "textures/blocks/magicRuneMid.png"),
		new ResourceLocation("tn3", "textures/blocks/magicRuneHigh.png")


};

}

 

 

Block:

 

 

package Technomage3.both.Blocks;

import java.util.Random;

import thaumcraft.common.config.ConfigBlocks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import am2.AMCore;
import am2.blocks.BlocksCommonProxy;
import am2.particles.AMParticle;
import am2.particles.ParticleFloatUpward;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChatMessageComponent;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import Technomage3.Core.Blocks.TN3Block;
import Technomage3.Core.Blocks.TN3BlockContainer;
import Technomage3.Core.Utils.TN3Utils;
import Technomage3.both.Blocks.tileentities.TileEntityMagicRune;
import Technomage3.both.Power.IUsePower;

public class BlockMagicRune extends TN3BlockContainer {

private Icon[] icons = new Icon[3];

public BlockMagicRune(String realName, String unlocalizedName, CreativeTabs tab) {
	super(realName, unlocalizedName, tab);
	this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.01F, 1.0F);
	this.setLightOpacity(0);
	this.setTickRandomly(true);
}

public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
{
	return AxisAlignedBB.getAABBPool().getAABB((double)par2 + super.minX, (double)par3 + super.minY, (double)par4 + super.minZ, (double)par2 + super.maxX, (double)par3 + super.maxY, (double)par4 + super.maxZ);
}

public void setBlockBoundsForItemRender()
{
	this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.01F, 1.0F);
}


public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int meta, float hitX, float hitY, float hitZ)
{

	TileEntityMagicRune tile = (TileEntityMagicRune) world.getBlockTileEntity(x, y, z);

	if(!world.isRemote){
		player.sendChatToPlayer(ChatMessageComponent.createFromTranslationKey(
				"Power Level: " + tile.getPowerLevel() + 
				", Power: " + tile.getPower()
				));
	}

	return true;
}

@Override
public TileEntity createNewTileEntity(World world) {
	return new TileEntityMagicRune();
}

public void randomDisplayTick(World world, int x, int y, int z, Random rand)
{
	if(world.isRemote && this.getPowerLevel(world, x, y, z) >= 50){

	} else if(world.isRemote && this.getPowerLevel(world, x, y, z) >= 0){


	} else{



	}
}

public int getPowerLevel(IBlockAccess world, int x, int y, int z) {

	TileEntityMagicRune tile = (TileEntityMagicRune) world.getBlockTileEntity(x, y, z);

	return tile.getPowerLevel();
}


/*
public Icon getIcon(int par1, int par2)
{
	return icons[1];
}
*/
public void registerIcons(IconRegister register)
{
	/*
	icons[2] = register.registerIcon(TN3Utils.getTexturePrefix() + "magicRuneHigh");

	icons[1] = register.registerIcon(TN3Utils.getTexturePrefix() + "magicRuneMid");

	icons[0] = register.registerIcon(TN3Utils.getTexturePrefix() + "magicRuneLow");
	*/
	blockIcon = register.registerIcon(TN3Utils.getTexturePrefix() + "magicRuneMid");

}

public void onBlockAdded(World world, int x, int y, int z) {

	TileEntityMagicRune rune = (TileEntityMagicRune) world.getBlockTileEntity(x, y, z);

	rune.onCreated();

}

public static boolean isNormalCube(int par0)
{
	return false;
}

public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean renderAsNormalBlock()
    {
        return false;
    }

    public int getRenderType()
    {
        return -1;
    }


}

 

 

Posted

You probably have z-fighting. That's when two textures are fighting to get rendered. You see stripes of both textures and they change if you move. You can fix that by changing the z-level.

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/

Posted

Thanks, I'm prety sure that's what it is, but now I'm using a model, which I like better anyway.

 

Does anyone know a way to get the texture not to float (like with redstone,where the texture is one pixel above the base block)?

Posted

The redstone block just has a small hitbox, and the part of the texture you see is the transparant part of the texture.

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.