Jump to content

Recommended Posts

Posted

My custom furnace that i'm making

is freezing my game!!

 

when i right click on it it will have major lag!

 

And. don't say i'm missing methods,

because i am, i'm not finished with it yet,

so don't say i'm missing methods please!

 

I'm experienced with java but not

too TOO advanced.

 

Main mod class:

 

package net.CraigTheMailman.CraftyMod;

 

import net.CraigTheMailman.CraftyMod.blocks.AlloyFurnace;

import net.CraigTheMailman.CraftyMod.handler.GuiHandler;

import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace;

import net.minecraft.block.Block;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.item.Item;

import cpw.mods.fml.common.Mod;

import cpw.mods.fml.common.Mod.EventHandler;

import cpw.mods.fml.common.Mod.Instance;

import cpw.mods.fml.common.event.FMLInitializationEvent;

import cpw.mods.fml.common.event.FMLPostInitializationEvent;

import cpw.mods.fml.common.event.FMLPreInitializationEvent;

import cpw.mods.fml.common.network.NetworkRegistry;

import cpw.mods.fml.common.registry.GameRegistry;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

@Mod(modid = CraftyMod.modid, name = "Crafty Mod", version = CraftyMod.version)

public class CraftyMod {

 

public static final String modid = "craftymod";

public static final String version = "Alpha v0.1";

 

//Creative Tab

public static CreativeTabs tabC;

 

@Instance(modid)

public static CraftyMod instance;

 

//Items

 

//Blocks

 

//Machines

public static Block blockAlloyFurnaceIdle;

public static Block blockAlloyFurnaceActive;

public static final int guiIDAlloyFurnace = 0;

 

@EventHandler

public void PreInit(FMLPreInitializationEvent preEvent) {

//Creative Tab

tabC = new CreativeTabs("CraftyMod") {

@Override

@SideOnly(Side.CLIENT)

public Item getTabIconItem() {

return Item.getItemFromBlock(blockAlloyFurnaceIdle);

}

 

};

 

//Initialization

//AlloyFurnace

blockAlloyFurnaceIdle = new AlloyFurnace(false).setCreativeTab(tabC).setBlockName("AlloyFurnace_off");

blockAlloyFurnaceActive = new AlloyFurnace(true).setBlockName("AlloyFurnace_on");

 

//GameReg

//Machines

GameRegistry.registerBlock(blockAlloyFurnaceIdle, "AlloyFurnace_off");

GameRegistry.registerBlock(blockAlloyFurnaceActive, "AlloyFurnace_on");

}

 

@EventHandler

public void Init(FMLInitializationEvent event) {

 

GameRegistry.registerTileEntity(TileEntityAlloyFurnace.class, "AlloyFurnace");

NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());

 

}

 

@EventHandler

public void PostInit(FMLPostInitializationEvent postEvent) {

 

}

}

 

 

 

Gui Handler:

 

package net.CraigTheMailman.CraftyMod.handler;

 

import net.CraigTheMailman.CraftyMod.CraftyMod;

import net.CraigTheMailman.CraftyMod.container.ContainerAlloyFurnace;

import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.ResourceLocation;

import net.minecraft.world.World;

import cpw.mods.fml.common.network.IGuiHandler;

 

public class GuiHandler implements IGuiHandler {

 

@Override

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

TileEntity entity = world.getTileEntity(x, y, z);

 

if(entity != null) {

switch(ID) {

case CraftyMod.guiIDAlloyFurnace:

if(entity instanceof TileEntityAlloyFurnace) {

return new ContainerAlloyFurnace(player.inventory,(TileEntityAlloyFurnace)entity);

}

return null;

}

}

return null;

}

 

@Override

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

TileEntity entity = world.getTileEntity(x, y, z);

 

if(entity != null) {

switch(ID) {

case CraftyMod.guiIDAlloyFurnace:

if(entity instanceof TileEntityAlloyFurnace) {

return new GuiAlloyFurnace(player.inventory,(TileEntityAlloyFurnace)entity);

}

return null;

}

}

return null;

}

 

}

 

 

 

Gui class for block:

 

package net.CraigTheMailman.CraftyMod.gui;

 

import org.lwjgl.opengl.GL11;

 

import net.CraigTheMailman.CraftyMod.CraftyMod;

import net.CraigTheMailman.CraftyMod.container.ContainerAlloyFurnace;

import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace;

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.inventory.GuiContainer;

import net.minecraft.client.resources.I18n;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.util.ResourceLocation;

 

public class GuiAlloyFurnace extends GuiContainer {

 

public static final ResourceLocation bground = new ResourceLocation(CraftyMod.modid + ":" + "textures/gui/guialloyfurnace.png");

 

public TileEntityAlloyFurnace alloyFurnace;

 

public GuiAlloyFurnace(InventoryPlayer inventoryPlayer, TileEntityAlloyFurnace entity) {

super(new ContainerAlloyFurnace(inventoryPlayer, entity));

 

this.alloyFurnace = entity;

 

this.xSize = 176;

this.ySize = 166;

}

 

@Override

public void drawGuiContainerForegroundLayer(int par1, int par2) {

String name = this.alloyFurnace.hasCustomInventoryName() ? this.alloyFurnace.getInventoryName() : I18n.format(this.alloyFurnace.getInventoryName(), new Object[0]);

 

this.fontRendererObj.drawString(name, this.xSize / 2 - this.fontRendererObj.getStringWidth(name) / 2, 6, 4210752);

this.fontRendererObj.drawString(I18n.format("container.inventory", new Object[0]), 128, this.ySize - 96 + 2, 4210752);

}

 

@Override

protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {

GL11.glColor4f(1F, 1F, 1F, 1F);

 

Minecraft.getMinecraft().getTextureManager().bindTexture(bground);

drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);

}

 

}

 

 

 

Block class:

 

package net.CraigTheMailman.CraftyMod.blocks;

 

import net.CraigTheMailman.CraftyMod.CraftyMod;

import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace;

import net.minecraft.block.Block;

import net.minecraft.block.BlockContainer;

import net.minecraft.block.material.Material;

import net.minecraft.client.renderer.texture.IIconRegister;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.tileentity.TileEntity;

import net.minecraft.util.IIcon;

import net.minecraft.util.MathHelper;

import net.minecraft.world.World;

import cpw.mods.fml.common.network.internal.FMLNetworkHandler;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class AlloyFurnace extends BlockContainer {

 

private final boolean isActive;

 

@SideOnly(Side.CLIENT)

private IIcon iconFront;

 

@SideOnly(Side.CLIENT)

private IIcon iconTop;

 

private static boolean keepInventory;

 

public AlloyFurnace(boolean isActive) {

super(Material.iron);

 

this.isActive = isActive;

 

}

 

@SideOnly(Side.CLIENT)

public void registerBlockIcons(IIconRegister iconRegister) {

this.blockIcon = iconRegister.registerIcon(CraftyMod.modid + ":" + "Alloy_side");

this.iconFront = iconRegister.registerIcon(CraftyMod.modid + ":" + "Alloy" + (this.isActive ? "_on" : "_off"));

this.iconTop = iconRegister.registerIcon(CraftyMod.modid + ":" + "Alloy_top_" + (this.isActive ? "on" : "off"));

}

 

@Override

@SideOnly(Side.CLIENT)

public IIcon getIcon(int side, int metadata) {

return metadata == 0 && side == 3 ? this.iconFront : side == 1 ? this.iconTop : (side == 0 ? this.iconTop : (side == metadata ? this.iconFront : this.blockIcon));

}

 

public Item getItemDropped(World world, int x, int y, int z) {

return Item.getItemFromBlock(CraftyMod.blockAlloyFurnaceIdle);

}

 

@Override

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

super.onBlockAdded(world, x, y, z);

this.setDefaultDirection(world, x, y, z);

}

 

private void setDefaultDirection(World world, int x, int y, int z) {

if(!world.isRemote) {

Block b1 = world.getBlock(x, y, z - 1);

Block b2 = world.getBlock(x, y, z + 1);

Block b3 = world.getBlock(x - 1, y, z);

Block b4 = world.getBlock(x + 1, y, z);

 

byte b0 = 3;

 

if(b1.func_149730_j() && !b2.func_149730_j()) {

b0 = 3;

}

 

if(b2.func_149730_j() && !b1.func_149730_j()) {

b0 = 2;

}

 

if(b3.func_149730_j() && !b4.func_149730_j()) {

b0 = 5;

}

 

if(b4.func_149730_j() && !b3.func_149730_j()) {

b0 = 4;

}

 

world.setBlockMetadataWithNotify(x, y, z, b0, 2);

}

 

}

 

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

if(!world.isRemote) {

FMLNetworkHandler.openGui(player, CraftyMod.instance, CraftyMod.guiIDAlloyFurnace, world, x, y, z);

}

return true;

}

 

@Override

public TileEntity createNewTileEntity(World var1, int var2) {

return new TileEntityAlloyFurnace();

}

 

//TODO randomDisplayTick

 

public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityplayer, ItemStack itemstack) {

int l = MathHelper.floor_double((double)(entityplayer.rotationYaw * 4.0F / 360F) + 0.5D) & 3;

 

if(l == 0) {

world.setBlockMetadataWithNotify(x, y, z, 2, 2);

}

 

if(l == 1) {

world.setBlockMetadataWithNotify(x, y, z, 5, 2);

}

 

if(l == 2) {

world.setBlockMetadataWithNotify(x, y, z, 3, 2);

}

 

if(l == 3) {

world.setBlockMetadataWithNotify(x, y, z, 4, 2);

}

 

if(itemstack.hasDisplayName()) {

((TileEntityAlloyFurnace)world.getTileEntity(x, y, z)).setGuiDisplayName(itemstack.getDisplayName());

}

}

 

public static void updateAlloyFurnaceBlockState(boolean active, World worldObj, int xCoord, int yCoord, int zCoord) {

int i = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);

 

TileEntity tileentity = worldObj.getTileEntity(xCoord, yCoord, zCoord);

keepInventory = true;

 

if(active) {

worldObj.setBlock(xCoord, yCoord, zCoord, CraftyMod.blockAlloyFurnaceActive);

}else {

worldObj.setBlock(xCoord, yCoord, zCoord, CraftyMod.blockAlloyFurnaceIdle);

}

 

keepInventory = false;

 

worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, i, 2);

 

if(tileentity != null) {

tileentity.validate();

worldObj.setTileEntity(xCoord, yCoord, zCoord, tileentity);

}

}

 

}

 

 

 

Container:

 

package net.CraigTheMailman.CraftyMod.container;

 

import net.CraigTheMailman.CraftyMod.tileentity.TileEntityAlloyFurnace;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.entity.player.InventoryPlayer;

import net.minecraft.inventory.Container;

import net.minecraft.inventory.ICrafting;

import net.minecraft.inventory.Slot;

import net.minecraft.inventory.SlotFurnace;

import cpw.mods.fml.relauncher.Side;

import cpw.mods.fml.relauncher.SideOnly;

 

public class ContainerAlloyFurnace extends Container {

 

private TileEntityAlloyFurnace alloyFurnace;

 

public int lastBurnTime;

public int lastCurrentItemBurnTime;

public int lastCookTime;

 

public ContainerAlloyFurnace(InventoryPlayer inventory, TileEntityAlloyFurnace tileentity) {

this.alloyFurnace = tileentity;

 

this.addSlotToContainer(new Slot(tileentity, 0, 56, 35));

this.addSlotToContainer(new Slot(tileentity, 1, 8, 62));

this.addSlotToContainer(new SlotFurnace(inventory.player, tileentity, 2, 116, 35));

 

for(int i = 0; i < 3; i++) {

for(int j = 0; j < 9; i++) {

this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 94 + i * 18));

}

}

 

for(int i = 0; i < 9; i++) {

this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142));

}

}

 

@Override

public void addCraftingToCrafters(ICrafting icrafting) {

super.addCraftingToCrafters(icrafting);

icrafting.sendProgressBarUpdate(this, 0, this.alloyFurnace.cookTime);

icrafting.sendProgressBarUpdate(this, 1, this.alloyFurnace.burnTime);

icrafting.sendProgressBarUpdate(this, 2, this.alloyFurnace.currentItemBurnTime);

}

 

@Override

public void detectAndSendChanges() {

super.detectAndSendChanges();

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

ICrafting icrafting = (ICrafting) this.crafters.get(i);

 

if(this.lastCookTime != this.alloyFurnace.cookTime) {

icrafting.sendProgressBarUpdate(this, 0, this.alloyFurnace.cookTime);

}

 

if(this.lastBurnTime != this.alloyFurnace.burnTime) {

icrafting.sendProgressBarUpdate(this, 1, this.alloyFurnace.burnTime);

}

 

if(this.lastCurrentItemBurnTime != this.alloyFurnace.currentItemBurnTime) {

icrafting.sendProgressBarUpdate(this, 2, this.alloyFurnace.currentItemBurnTime);

}

}

 

this.lastCookTime = this.alloyFurnace.cookTime;

this.lastBurnTime = this.alloyFurnace.burnTime;

this.lastCurrentItemBurnTime = this.alloyFurnace.currentItemBurnTime;

}

 

@Override

@SideOnly(Side.CLIENT)

public void updateProgressBar(int slot, int newValue) {

 

}

 

@Override

public boolean canInteractWith(EntityPlayer var1) {

return true;

}

 

}

 

 

 

Tile Entity:

 

package net.CraigTheMailman.CraftyMod.tileentity;

 

import net.CraigTheMailman.CraftyMod.blocks.AlloyFurnace;

import net.minecraft.block.Block;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.Blocks;

import net.minecraft.init.Items;

import net.minecraft.inventory.ISidedInventory;

import net.minecraft.item.Item;

import net.minecraft.item.ItemBlock;

import net.minecraft.item.ItemStack;

import net.minecraft.item.crafting.FurnaceRecipes;

import net.minecraft.tileentity.TileEntity;

import cpw.mods.fml.common.registry.GameRegistry;

public class TileEntityAlloyFurnace extends TileEntity implements ISidedInventory {

 

private String localizeName;

 

private ItemStack[] slots = new ItemStack[3];

 

public int furnaceSpeed = 200;

public int burnTime;

public int currentItemBurnTime;

public int cookTime;

 

private static final int[] slots_top = new int[]{0};

private static final int[] slots_bottom = new int[]{2, 1};

private static final int[] slots_side = new int[]{1};

 

 

public void setGuiDisplayName(String displayName) {

this.localizeName = displayName;

 

}

 

public String getInventoryName() {

return this.hasCustomInventoryName() ? this.localizeName : "container.alloyFurnace";

}

 

public boolean hasCustomInventoryName() {

return this.localizeName != null && this.localizeName.length() > 0;

}

 

public int getSizeInventory() {

return this.slots.length;

}

 

@Override

public ItemStack getStackInSlot(int var1) {

return this.slots[var1];

}

 

@Override

public ItemStack decrStackSize(int var1, int var2) {

if(this.slots[var1] != null) {

ItemStack itemstack;

 

if(this.slots[var1].stackSize <= var2) {

itemstack = this.slots[var1];

this.slots[var1] = null;

return itemstack;

}else{

itemstack = this.slots[var1].splitStack(var2);

 

if(this.slots[var1].stackSize == 0) {

this.slots[var1] = null;

}

}

}

 

return null;

}

 

@Override

public ItemStack getStackInSlotOnClosing(int i) {

if(this.slots != null) {

ItemStack itemstack = this.slots;

this.slots = null;

return itemstack;

}

return null;

}

 

@Override

public void setInventorySlotContents(int i, ItemStack itemstack) {

this.slots = itemstack;

 

if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()) {

itemstack.stackSize = this.getInventoryStackLimit();

}

 

}

 

@Override

public int getInventoryStackLimit() {

return 64;

}

 

@Override

public boolean isUseableByPlayer(EntityPlayer player) {

return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;

}

 

@Override

public void openInventory() {}

@Override

public void closeInventory() {}

 

@Override

public boolean isItemValidForSlot(int i, ItemStack itemstack) {

return i == 2 ? false :(i == 1 ? isItemFuel(itemstack) : true);

}

 

public static boolean isItemFuel(ItemStack itemstack) {

return getItemBurnTime(itemstack) > 0;

}

 

private static int getItemBurnTime(ItemStack itemstack) {

 

if(itemstack == null) {

return 0;

}else {

Item item = itemstack.getItem();

 

if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {

Block block = Block.getBlockFromItem(item);

 

//if(item == (item goes here) return (ticks);

 

return GameRegistry.getFuelValue(itemstack);

}

}

 

return 0;

}

 

public boolean isBurning() {

return this.burnTime > 0;

}

 

@Override

public void updateEntity() {

boolean flag = this.burnTime > 0;

boolean flag1 = false;

 

if(this.isBurning()) {

this.burnTime--;

}

 

if(!this.worldObj.isRemote) {

if(this.burnTime == 0 && this.canSmelt()) {

this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[1]);

 

if(this.isBurning()) {

flag1 = true;

 

if(this.slots[1] != null) {

this.slots[1].stackSize--;

 

if(this.slots[1].stackSize == 0) {

this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);

}

}

}

}

 

if(this.isBurning() && this.canSmelt()) {

this.cookTime++;

 

if(this.cookTime == this.furnaceSpeed) {

this.cookTime = 0;

this.smeltItem();

flag1 = true;

}

}else {

this.cookTime = 0;

}

 

if(flag != this.isBurning()) {

flag1 = true;

AlloyFurnace.updateAlloyFurnaceBlockState(this.burnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);

}

}

 

if(flag1) {

this.markDirty();

}

}

 

public boolean canSmelt() {

if(this.slots[0] == null) {

return false;

}else {

ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);

 

if(itemstack == null) return false;

if(this.slots[2] == null) return true;

if(this.slots[2].isItemEqual(itemstack)) return false;

 

int result = this.slots[2].stackSize + itemstack.stackSize;

 

return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());

 

}

 

}

 

public void smeltItem() {

if(this.canSmelt()) {

ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.slots[0]);

 

if(this.slots[2] == null) {

this.slots[2] = itemstack.copy();

}else if(this.slots[2].isItemEqual(itemstack)) {

this.slots[2].stackSize += itemstack.stackSize;

}

 

this.slots[0].stackSize--;

 

if(this.slots[0].stackSize <= 0) {

this.slots[0] = null;

}

}

}

 

@Override

public int[] getAccessibleSlotsFromSide(int var1) {

return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_side);

}

 

@Override

public boolean canInsertItem(int i, ItemStack itemstack, int j) {

return this.isItemValidForSlot(i, itemstack);

}

 

@Override

public boolean canExtractItem(int i, ItemStack itemstack, int j) {

return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;

}

 

}

 

 

Posted

One thing to note.

Don't register or create blocks/items during class construction <clinit>.

 

You need to do them like you did with your creative tab.

 

Example:

public static Block block;
@EventHandler
public void preInit(PreInitializationEvent event) {
block = new TutorialBlock().setUnlocalizedName("tutBlock");
}

//Note: This is an "example."\\

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

    • 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?
    • Remove rubidium - you are already using embeddium, which is a fork of rubidium
  • Topics

×
×
  • Create New...

Important Information

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