Jump to content

Recommended Posts

Posted

How would I go about doing the block class?

 

I see what you mean. That blog post should be properly titled, a multi-TE TileEntity, since there are no blocks involved in their code.

Posted

I'm a fan of just doing it with good ol' "brute force".  Just take some graph paper and draw out what you want.  Then create a method that places each block in the location (probably a relative location to some starting point).

 

For any regularly shaped features, like a straight wall, you can use for loops to cut down on the lines of code you need.

 

I actually do it with 2D arrays for each layer, and then loop through the arrays.  For example, imagine if you set a int code to represent each type of block you want.  Like air/nothing = 0, sandstone = 1, or whatever blocks you are happening to use.

 

Then you can have an int array for first layer that sets the foundation for the walls, something like this for a room:

int[][] wallsRoom1 = new int[][] 
{{1,1,1,1,1,1,1},
  {1,0,0,0,0,0,1},
  {1,0,0,0,0,0,1},  
  {1,0,0,0,0,0,1},  
  {1,0,0,0,0,0,1},  
  {1,1,1,0,1,1,1},  
  {0,0,1,0,1,0,0},
  {0,0,1,0,1,0,0}};

 

And then you can loop through that placing the block indicated in the location indicated.  You could build up this wall by looping through at multiple heights.

 

I like this method because it is almost like just drawing the structure right in code -- very easy to visualize.

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Guy who wrote the tutorial here. Just finished fixing some errors in it. What don't you seem to get?

 

Thank you for your tutorial, it helps me (who is new to modding) get the tile entity class. But I don't get the Block class and how to decide each block, and where they go, and then call it. I've tried some methods that didn't work and got frustrated before deleting that code and writing new code that still won't work. Any help is appreciated!

Posted

Just have your Block class either extend BlockContainer (instead of simply Block) or implement ITileEntityProvider. It'll generate a method called createNewTileEntity, which should be as such:

public TileEntity createNewTileEntity(World world, int meta) {
    return new TileMultiBlock(); //or whatever you name your tile entity class
}

 

Also remember to register your TileEntity, or else it will not work

//Recommend doing this somewhere in your FMLInitializationEvent.
GameRegistry.registerTileEntity(TileMultiBlock.class, "modmod.tilemultiblock");

Posted

Just have your Block class either extend BlockContainer (instead of simply Block) or implement ITileEntityProvider. It'll generate a method called createNewTileEntity, which should be as such:

public TileEntity createNewTileEntity(World world, int meta) {
    return new TileMultiBlock(); //or whatever you name your tile entity class
}

 

Also remember to register your TileEntity, or else it will not work

//Recommend doing this somewhere in your FMLInitializationEvent.
GameRegistry.registerTileEntity(TileMultiBlock.class, "modmod.tilemultiblock");

 

I am sorry to be such a noob, but where and how do I specify my specific blocks I am using and the location of each one?

Posted

I am sorry to be such a noob, but where and how do I specify my specific blocks I am using and the location of each one?

In the checkMultiBlockForm method in the Tile Entity. ATM, the way the tutorial is written, it checks for blocks that have the same tile entity (so if you want all your multiblocks to be made of the same block, the tutorial has it already set up). If you want to check for specific blocks, you'd have to do that manually, and change setupStructure method accordingly.

Posted

How would I do an onBlockActivated of the MultiBlock = player.openGui?

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
    if (!world.isRemote && !player.isSneaking()) {
        TileMultiBlock tile = (TileMultiBlock) world.getTileEntity(x, y, z);
        if (tile != null) {
            if (tile.hasMaster()) {
                if (tile.isMaster())
                    player.openGui(Mod.instance, 99, world, x, y, z);
                else
                    player.openGui(Mod.instance, 99, world, tile.getMasterX(), tile.getMasterY(), tile.getMasterZ());
                return true;
            }
        }
    }
    return false;
}

Replace the values accordingly.

Posted

Why is everyone requesting code when i gave them a good tutorial for themto use? The only thing you have to do is make the block class, and that's explained in the tutorial as well (the opening-gui part). More then that you don't need.

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

Why am I getting an error: "The method hasMaster() is undefined for the type TileEntity" on

 if (this.isMaster()) {
                            if (tile.hasMaster())
                                i++;
                        } else if (!tile.hasMaster())
                            i++;

Posted

Why am I getting an error: "The method hasMaster() is undefined for the type TileEntity" on

 if (this.isMaster()) {
                            if (tile.hasMaster())
                                i++;
                        } else if (!tile.hasMaster())
                            i++;

Opps, another derp on my part, cast the tiles as instanceof of TileMultiBlock (or whatever you named your class)

Posted

I'm going further on what Lomeli12 has said:

He has showed you how to create a tile entity.

--Make sure it is registered by the game.

 

Create a Block Class which extends the BlockContainer Class:

Below is an example, I have to it passing the x, y, z co-ordinates which the block was placed.

 

package com.example.examplemod.blocks;

import com.example.examplemod.entity.TileMultiBlock;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class BlockMainBlock extends BlockContainer {

       // Normal setting up block stuff
public BlockMainBlock() {
	super(Material.rock);
	setBlockName("mainBlock");
	setCreativeTab(CreativeTabs.tabBlock);
}

        // Called when block is placed
@Override
public void onBlockAdded(World world, int x, int y, int z) {
	this.x = x;
	this.y = y;
	this.z = z;
}

        // Method you must use to create a new TileEntity.
@Override
public TileEntity createNewTileEntity(World world, int p_149915_2_) {

	return new TileMultiBlock(x, y, z);
}	
}

 

Later I'll get back to you on detecting stone.. :)

 

Thank You Lomeli12, For your tutorial! Helped me a ton! :)

Posted

I'm going further on what Lomeli12 has said:

He has showed you how to create a tile entity.

--Make sure it is registered by the game.

 

Create a Block Class which extends the BlockContainer Class:

Below is an example, I have to it passing the x, y, z co-ordinates which the block was placed.

 

package com.example.examplemod.blocks;

import com.example.examplemod.entity.TileMultiBlock;

import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class BlockMainBlock extends BlockContainer {

       // Normal setting up block stuff
public BlockMainBlock() {
	super(Material.rock);
	setBlockName("mainBlock");
	setCreativeTab(CreativeTabs.tabBlock);
}

        // Called when block is placed
@Override
public void onBlockAdded(World world, int x, int y, int z) {
	this.x = x;
	this.y = y;
	this.z = z;
}

        // Method you must use to create a new TileEntity.
@Override
public TileEntity createNewTileEntity(World world, int p_149915_2_) {

	return new TileMultiBlock(x, y, z);
}	
}

 

Thank you, can't wait for that next part :)

Posted

This doesn't work...I did try again though

 

Block Class

 

 

public class Forge extends BlockContainer{

public Forge(int id, Material material){
	super(material);
	this.setCreativeTab(TestCraft.TestCraftTab);
}

public void onBlockAdded(World world, int x, int y, int z){
	TileEntityForge tile = new TileEntityForge();
	if(!tile.shouldRender){
		if(world.getBlock(x, y, z) == Blocks.stone
    			&& world.getBlock(x, y-1, z) == Blocks.stone
    			&& world.getBlock(x, y+1, z) == Blocks.stone
    			&& world.getBlock(x-1, y, z) == Blocks.stone
    			&& world.getBlock(x-1, y-1, z) == Blocks.stone
    			&& world.getBlock(x-1, y+1, z) == Blocks.stone
    			&& world.getBlock(x+1, y, z) == Blocks.stone
    			&& world.getBlock(x+1, y-1, z) == Blocks.stone
    			&& world.getBlock(x+1, y+1, z) == Blocks.stone){
			System.out.println("I am put together!");
    		((TileEntityForge)tile).shouldRender = true; 
		}
	}

}

@Override
public TileEntity createNewTileEntity(World var1, int var2) {
	return new TileEntityForge();
}
}

 

 

 

And the TileEntity Class

 

 

public class TileEntityForge extends TileEntity{
public boolean shouldRender = false;

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

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

@Override
public void readFromNBT(NBTTagCompound tagCompound)
{
	super.readFromNBT(tagCompound);
	this.shouldRender = tagCompound.getBoolean("shouldRender");
}

@Override
public void writeToNBT(NBTTagCompound tagCompound)
{
	super.writeToNBT(tagCompound);
	tagCompound.setBoolean("shouldRender", this.shouldRender);
}
}

 

 

 

Any help? I have tried many methods...

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.