Jump to content

Recommended Posts

Posted

Hey I am trying to make a mod for my daughter so she can make textures/designs for it. However I have no programming experience and have run into an issue I cant figure out. I am creating a set of metadata blocks and they all show the different textures in the inventory. The issue is there are only two names from the first 2 items. I have 1 Black Dyed Stone and 14 Blue Dyed Stone. Also because of this only the black and blue stones can be placed in the world. I can change the stone names for the two that appear as well as the textures that appear in the world in the areas that I believe the problem is in but cannot figure out how to add more names to the list without errors.

 

StoneBLocksProperties.java

package com.panex.coloryourworld.blocks;

import java.util.List;

import com.panex.coloryourworld.creativetabs.MTabs;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

public class StoneBlockProperties extends Block implements IMetaBlockName {

public static final PropertyEnum TYPE = PropertyEnum.create("type", StoneBlockProperties.EnumType.class);

public StoneBlockProperties(String unlocalizedName) {
	super(Material.iron);
	this.setUnlocalizedName(unlocalizedName);
	this.setCreativeTab(MTabs.tabblock);
	this.setHardness(2);
	this.setResistance(6);
	this.setHarvestLevel("pickaxe", 0);
	this.setLightOpacity(16);
	this.setLightLevel(0);
	this.setDefaultState(this.blockState.getBaseState().withProperty(TYPE, EnumType.BLACK));
}

@Override
public IBlockState getStateFromMeta(int meta) {
	return this.getDefaultState().withProperty(TYPE, meta == 0 ? EnumType.BLACK : EnumType.BLUE) ;
			}

@Override
public int getMetaFromState(IBlockState state) {
	EnumType type = (EnumType) state.getValue(TYPE);
	return type.getID();
}

@Override
public int damageDropped(IBlockState state) {
	return getMetaFromState(state);
}

@Override
protected BlockState createBlockState() {
	return new BlockState(this, new IProperty[] { TYPE });
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void getSubBlocks(Item itemIn, CreativeTabs tab, List list) {
	list.add(new ItemStack(itemIn, 1, 0));
	list.add(new ItemStack(itemIn, 1, 1));
	list.add(new ItemStack(itemIn, 1, 2));
	list.add(new ItemStack(itemIn, 1, 3));
	list.add(new ItemStack(itemIn, 1, 4));
	list.add(new ItemStack(itemIn, 1, 5));
	list.add(new ItemStack(itemIn, 1, 6));
	list.add(new ItemStack(itemIn, 1, 7));
	list.add(new ItemStack(itemIn, 1, );
	list.add(new ItemStack(itemIn, 1, 9));
	list.add(new ItemStack(itemIn, 1, 10));
	list.add(new ItemStack(itemIn, 1, 11));
	list.add(new ItemStack(itemIn, 1, 12));
	list.add(new ItemStack(itemIn, 1, 13));
	list.add(new ItemStack(itemIn, 1, 14));

}

@Override
public String getSpecialName(ItemStack stack) {
	return stack.getItemDamage() == 0 ? "black" :  "blue";  

}

@Override
public ItemStack getPickBlock(MovingObjectPosition target, World world, BlockPos pos) {
	return new ItemStack(Item.getItemFromBlock(this), 1, this.getMetaFromState(world.getBlockState(pos)));
}

public enum EnumType implements IStringSerializable {
	BLACK(0, "black"), BLUE(1, "blue"), BROWN(2, "brown"), CYAN(3, "cyan"), GREEN(4, "green"), LIGHTBLUE(5, "lightblue"), LIGHTGRAY(6, "lightgray"), LIME(7, "lime"), MAGENTA(8, "magenta"), ORANGE(9, "orange"), PINK(10, "pink"), PURPLE(11, "purple"), RED(12, "red"), WHITE(13, "white"), YELLOW(14, "yellow");

	private int ID;
	private String name;

	private EnumType(int ID, String name) {
		this.ID = ID;
		this.name = name;
	}

	@Override
	public String getName() {
		return this.name;
	}

	public int getID() {
		return this.ID;
	}

	@Override
	public String toString() {
		return this.getName();
	}
}
}

 

IMetaBlockName.java

package com.panex.coloryourworld.blocks;

import net.minecraft.item.ItemStack;

public interface IMetaBlockName {

String getSpecialName(ItemStack stack);
}

 

ItemBlockMeta.java

package com.panex.coloryourworld.blocks;

import net.minecraft.block.Block;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;

public class ItemBlockMeta extends ItemBlock {

public ItemBlockMeta(Block block) {
	super(block);
	if (!(block instanceof IMetaBlockName)) {
		throw new IllegalArgumentException(String.format("The given Block %s is not an instance of ISpecialBlockName!", block.getUnlocalizedName()));
	}
	this.setMaxDamage(0);
        this.setHasSubtypes(true);
    }

    @Override
public int getMetadata(int damage)
    {
        return damage;
    }

@Override
public String getUnlocalizedName(ItemStack stack) {
	return super.getUnlocalizedName(stack) + "." + ((IMetaBlockName)this.block).getSpecialName(stack);
}
}

 

MBlocks.java

package com.panex.coloryourworld.blocks;

import com.panex.coloryourworld.items.MItems;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraftforge.fml.common.registry.GameRegistry;

public final class MBlocks {

//gray stone
public static Block stoneBlock;


//gem blocks
 public static Block ruby_block;
 public static Block amethyst_block;    
 public static Block amber_block;
 public static Block aquamarine_block;
 public static Block lepidolite_block;
 public static Block onyx_block;
 public static Block opal_block;
 public static Block peridot_block;
 public static Block rubellite_block;
 public static Block sapphire_block;
 public static Block silvertopaz_block;
 public static Block tigerseye_block;
 public static Block topaz_block;
 public static Block tourmaline_block;

//gem ore
    public static Block ruby_ore;
    public static Block amethyst_ore;    
    public static Block amber_ore;
    public static Block aquamarine_ore;
    public static Block lepidolite_ore;
    public static Block onyx_ore;
    public static Block opal_ore;
    public static Block peridot_ore;
    public static Block rubellite_ore;
    public static Block sapphire_ore;
    public static Block silvertopaz_ore;
    public static Block tigerseye_ore;
    public static Block topaz_ore;
    public static Block tourmaline_ore;


    public static void createBlocks() {
    	
    	//gray stone
    	GameRegistry.registerBlock(stoneBlock = new StoneBlockProperties("stoneblock_properties"), ItemBlockMeta.class, "stoneblock_properties");
    	
    	//gem block
    	GameRegistry.registerBlock(amethyst_block = new GemBlock("amethyst_block", Material.rock), "amethyst_block");
    	GameRegistry.registerBlock(amber_block = new GemBlock("amber_block", Material.rock), "amber_block");
    	GameRegistry.registerBlock(aquamarine_block = new GemBlock("aquamarine_block", Material.rock), "aquamarine_block");
    	GameRegistry.registerBlock(lepidolite_block = new GemBlock("lepidolite_block", Material.rock), "lepidolite_block");
    	GameRegistry.registerBlock(onyx_block = new GemBlock("onyx_block", Material.rock), "onyx_block");
    	GameRegistry.registerBlock(opal_block = new GemBlock("opal_block", Material.rock), "opal_block");
    	GameRegistry.registerBlock(peridot_block = new GemBlock("peridot_block", Material.rock), "peridot_block");
    	GameRegistry.registerBlock(rubellite_block = new GemBlock("rubellite_block", Material.rock), "rubellite_block");
    	GameRegistry.registerBlock(ruby_block = new GemBlock("ruby_block", Material.rock), "ruby_block");
    	GameRegistry.registerBlock(sapphire_block = new GemBlock("sapphire_block", Material.rock), "sapphire_block");
    	GameRegistry.registerBlock(silvertopaz_block = new GemBlock("silvertopaz_block", Material.rock), "silvertopaz_block");
    	GameRegistry.registerBlock(tigerseye_block = new GemBlock("tigerseye_block", Material.rock), "tigerseye_block");
    	GameRegistry.registerBlock(topaz_block = new GemBlock("topaz_block", Material.rock), "topaz_block");
    	GameRegistry.registerBlock(tourmaline_block = new GemBlock("tourmaline_block", Material.rock), "tourmaline_block");


    	
    	//gem ore
    	GameRegistry.registerBlock(amethyst_ore = new OreBlock("amethyst_ore", Material.rock, MItems.amethystgem, 1, 2), "amethyst_ore");
    	GameRegistry.registerBlock(ruby_ore = new OreBlock("ruby_ore", Material.rock, MItems.rubygem, 1, 2), "ruby_ore");    	
    	GameRegistry.registerBlock(amber_ore = new OreBlock("amber_ore", Material.rock, MItems.ambergem, 1, 2), "amber_ore");
    	GameRegistry.registerBlock(aquamarine_ore = new OreBlock("aquamarine_ore", Material.rock, MItems.aquamarinegem, 1, 2), "aquamarine_ore");
    	GameRegistry.registerBlock(lepidolite_ore = new OreBlock("lepidolite_ore", Material.rock, MItems.lepidolitegem, 1, 2), "lepidolite_ore");
    	GameRegistry.registerBlock(onyx_ore = new OreBlock("onyx_ore", Material.rock, MItems.onyxgem, 1, 2), "onyx_ore");
    	GameRegistry.registerBlock(opal_ore = new OreBlock("opal_ore", Material.rock, MItems.opalgem, 1, 2), "opal_ore");
    	GameRegistry.registerBlock(peridot_ore = new OreBlock("peridot_ore", Material.rock, MItems.peridotgem, 1, 2), "peridot_ore");
    	GameRegistry.registerBlock(rubellite_ore = new OreBlock("rubellite_ore", Material.rock, MItems.rubellitegem, 1, 2), "rubellite_ore");
    	GameRegistry.registerBlock(sapphire_ore = new OreBlock("sapphire_ore", Material.rock, MItems.sapphiregem, 1, 2), "sapphire_ore");
    	GameRegistry.registerBlock(silvertopaz_ore = new OreBlock("silvertopaz_ore", Material.rock, MItems.silvertopazgem, 1, 2), "silvertopaz_ore");
    	GameRegistry.registerBlock(tigerseye_ore = new OreBlock("tigerseye_ore", Material.rock, MItems.tigerseyegem, 1, 2), "tigerseye_ore");
    	GameRegistry.registerBlock(topaz_ore = new OreBlock("topaz_ore", Material.rock, MItems.topazgem, 1, 2), "topaz_ore");
    	GameRegistry.registerBlock(tourmaline_ore = new OreBlock("tourmaline_ore", Material.rock, MItems.tourmalinegem, 1, 2), "tourmaline_ore");

    }
}

 

BlockRenderRegister.java

package com.panex.coloryourworld.client.render.blocks;

import com.panex.coloryourworld.Main;
import com.panex.coloryourworld.blocks.MBlocks;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;

public final class BlockRenderRegister {

public static void preInit() {
	ModelBakery.addVariantName(Item.getItemFromBlock(MBlocks.stoneBlock),
			"coloryourworld:stoneblock_properties_black", 
			"coloryourworld:stoneblock_properties_blue", 
			"coloryourworld:stoneblock_properties_brown", 
			"coloryourworld:stoneblock_properties_cyan", 
			"coloryourworld:stoneblock_properties_green", 
			"coloryourworld:stoneblock_properties_lightblue",
			"coloryourworld:stoneblock_properties_lightgray", 
			"coloryourworld:stoneblock_properties_lime", 
			"coloryourworld:stoneblock_properties_magenta",
			"coloryourworld:stoneblock_properties_orange",
			"coloryourworld:stoneblock_properties_pink", 
			"coloryourworld:stoneblock_properties_purple",
			"coloryourworld:stoneblock_properties_red", 
			"coloryourworld:stoneblock_properties_white",
			"coloryourworld:stoneblock_properties_yellow" );
}

 public static void registerBlockRenderer() {

	 //stone blocks
	 	reg(MBlocks.stoneBlock, 0, "stoneblock_properties_black");
		reg(MBlocks.stoneBlock, 1, "stoneblock_properties_blue");
		reg(MBlocks.stoneBlock, 2, "stoneblock_properties_brown");
		reg(MBlocks.stoneBlock, 3, "stoneblock_properties_cyan");
		reg(MBlocks.stoneBlock, 4, "stoneblock_properties_green");
		reg(MBlocks.stoneBlock, 5, "stoneblock_properties_lightblue");
		reg(MBlocks.stoneBlock, 6, "stoneblock_properties_lightgray");
		reg(MBlocks.stoneBlock, 7, "stoneblock_properties_lime");
		reg(MBlocks.stoneBlock, 8, "stoneblock_properties_magenta");
		reg(MBlocks.stoneBlock, 9, "stoneblock_properties_orange");
		reg(MBlocks.stoneBlock, 10, "stoneblock_properties_pink");
		reg(MBlocks.stoneBlock, 11, "stoneblock_properties_purple");
		reg(MBlocks.stoneBlock, 12, "stoneblock_properties_red");
		reg(MBlocks.stoneBlock, 13, "stoneblock_properties_white");
		reg(MBlocks.stoneBlock, 14, "stoneblock_properties_yellow");



	 //gem block
	 	reg(MBlocks.ruby_block);
		reg(MBlocks.amethyst_block);			
		reg(MBlocks.amber_block);
		reg(MBlocks.aquamarine_block);
		reg(MBlocks.lepidolite_block);
		reg(MBlocks.onyx_block);
		reg(MBlocks.opal_block);
		reg(MBlocks.peridot_block);
		reg(MBlocks.rubellite_block);
		reg(MBlocks.sapphire_block);
		reg(MBlocks.silvertopaz_block);
		reg(MBlocks.tigerseye_block);
		reg(MBlocks.topaz_block);
		reg(MBlocks.tourmaline_block);

	 //gem ore
		reg(MBlocks.ruby_ore);
		reg(MBlocks.amethyst_ore);			
		reg(MBlocks.amber_ore);
		reg(MBlocks.aquamarine_ore);
		reg(MBlocks.lepidolite_ore);
		reg(MBlocks.onyx_ore);
		reg(MBlocks.opal_ore);
		reg(MBlocks.peridot_ore);
		reg(MBlocks.rubellite_ore);
		reg(MBlocks.sapphire_ore);
		reg(MBlocks.silvertopaz_ore);
		reg(MBlocks.tigerseye_ore);
		reg(MBlocks.topaz_ore);
		reg(MBlocks.tourmaline_ore);
	}

	public static String modid = Main.MODID;

	public static void reg(Block block) {
		Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), 0, new ModelResourceLocation(modid + ":" + block.getUnlocalizedName().substring(5), "inventory"));
	}
	public static void reg(Block block, int meta, String file) {
		Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), meta, new ModelResourceLocation(modid + ":" + file, "inventory"));
	}
}

 

Lang

tile.stoneblock_properties.black.name=Black Dyed Stone
tile.stoneblock_properties.blue.name=Blue Dyed Stone
tile.stoneblock_properties.brown.name=Brown Dyed Stone
tile.stoneblock_properties.cyan.name=Cyan Dyed Stone
tile.stoneblock_properties.green.name=Green Dyed Stone
tile.stoneblock_properties.lightblue.name=Light Blue Dyed Stone
tile.stoneblock_properties.lightgray.name=Light Gray Dyed Stone
tile.stoneblock_properties.lime.name=Lime Dyed Stone
tile.stoneblock_properties.magenta.name=Magenta Dyed Stone
tile.stoneblock_properties.orange.name=Orange Dyed Stone
tile.stoneblock_properties.pink.name=Pink Dyed Stone
tile.stoneblock_properties.purple.name=Purple Dyed Stone
tile.stoneblock_properties.red.name=Red Dyed Stone
tile.stoneblock_properties.white.name=White Dyed Stone
tile.stoneblock_properties.yellow.name=Yellow Dyed Stone

 

blockstates/stoneblock_properties.json

 

{
    "variants": {
        "type=black": { "model":"coloryourworld:stoneblock_properties_black" },
        "type=blue": { "model":"coloryourworld:stoneblock_properties_blue" },
"type=brown": { "model":"coloryourworld:stoneblock_properties_brown" },
"type=cyan": { "model":"coloryourworld:stoneblock_properties_cyan" },
"type=green": { "model":"coloryourworld:stoneblock_properties_green" },
"type=lightblue": { "model":"coloryourworld:stoneblock_properties_lightblue" },
"type=lightgray": { "model":"coloryourworld:stoneblock_properties_lightgray" },
"type=lime": { "model":"coloryourworld:stoneblock_properties_lime" },
"type=magenta": { "model":"coloryourworld:stoneblock_properties_magenta" },
"type=orange": { "model":"coloryourworld:stoneblock_properties_orange" },
"type=pink": { "model":"coloryourworld:stoneblock_properties_pink" },
"type=purple": { "model":"coloryourworld:stoneblock_properties_purple" },
"type=red": { "model":"coloryourworld:stoneblock_properties_red" },
"type=white": { "model":"coloryourworld:stoneblock_properties_white" },
"type=yellow": { "model":"coloryourworld:stoneblock_properties_yellow" }
    }
}

 

model/blocks stoneblock_properties_black.json

this is one of the 15 files all the rest are the same except for the color names change both in name of file and inside the file.

 

{
"parent":"block/cube_all",
"textures": {
	"all": "coloryourworld:blocks/stoneblock_properties_black"
}
}

 

model/items stoneblock_properties_black.json

 

this is one of the 15 files all the rest are the same except for the color names change both in name of file and inside the file.

 

{
    "parent":"coloryourworld:block/stoneblock_properties_black",
    "display": {
        "thirdperson": {
            "rotation": [ 10, -45, 170 ],
            "translation": [ 0, 1.5, -2.75 ],
            "scale": [ 0.375, 0.375, 0.375 ]
        }
    }
}

 

If I have forgotten to put in anything that will help please let me know and I will get it posted. Also like I said I am new to programming so the dumber the explanation can be the better. I really appreciate any help that can be given. Thank you.

 

 

 

 

 

 

 

 

 

 

 

You mean, you put down your rock and I put down my sword, and we try kill each other like civilized people?

-Westley

Posted

I found that problem and its the area I mentioned that I could change the names of the enumtypes but for the life of me I cant figure out how to add more to it. Everytime I try adding the other names to that list I get errors to the whole section of code. If I change one of the existing ones it does change the block texture placed in game. Also the same goes for the .getspecialname if I change the types there it changes the names but if I try to add more I get errors on that entire section of code as well.

You mean, you put down your rock and I put down my sword, and we try kill each other like civilized people?

-Westley

Posted

Then you need to Google the syntax of Java enum. After fixing that (or deciding to use one of vanilla Minecraft's existing color types), then replace your expression in the state from meta method.

 

Get rid of the trinary operator. I recommend using an enum's built in values array to return the enum value for a given integer.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Posted

Thank both of you very much. I will go see if I can get it figured out with this info.

You mean, you put down your rock and I put down my sword, and we try kill each other like civilized people?

-Westley

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

    • 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.