Jump to content

Recommended Posts

Posted

So I just finished a simple mod that creates and plays a game of Tic Tac Toe in the world.  The board, made of 9 blocks containing their tile entities is created by using a ttt item.  This is my first attempt at something more complicated than a block or item and with code that requires multiple parts talking to each other.  I am sure there are better ways to do a lot of what I have done, so I am posting all of the code here for anyone to review and critique.

 

Main file:

 

  Reveal hidden contents

 

 

Block files:

 

  Reveal hidden contents

 

 

 

  Reveal hidden contents

 

 

Entity:

 

  Reveal hidden contents

 

 

 

  Reveal hidden contents

 

 

Tile Entity:

 

  Reveal hidden contents

 

 

Item:

 

  Reveal hidden contents

 

 

 

  Reveal hidden contents

 

Posted

I don't understand why you used an Item and an Entity.

You can place the blocks themselves, and they already contain the TileEntity.

 

My advice:

Merge the entity-placing-blocks code into the "item use" method, then move that content into the "block placed" method, finally delete everything about the item and the entity. The TileEntity should store everything related to the play.

Not optimal, but better.

Then you could improve the thing by using only one TileEntity, for example in the middle block.

With two block states based on metadata, only one having the TileEntity. (do not use BlockContainer, prefer hasTileEntity(metadata)...)

The "block placed" method would check around for other similar blocks, and set the middle block to the state with a TileEntity, when the board is complete.

The "block activated" method would check around for a block with the tileentity, and write to it.

Posted

My thought behind the Entity itself was a central place to keep track of the game.  For TTT, a rather simple game for sure, it is keeping track of whose turn it is, so that clicking on any of the blocks creates the appropriate X or O.  I wasn't sure of the best way to get a blocks to find a TileEntity to figure out whose turn it was.

Posted

Yes, I know it isn't saving it, I wasn't concerned with this particular version with that part.  I meant keeping track so that when one cube is activated it is X and the next cube is O, etc. 

Posted

You could do something like this in your TileEntity:

private boolean mark = true;
private String[] players = new String[2];

public boolean getNextMark(){//returns a boolean opposite each time, equivalent to 0 or X
    mark = !mark;
    return mark;
}

public boolean isNextPlayer(EntityPlayer player){//check that a player can play this game, on this turn
    return player.username.equals(players[mark?0:1]);
}

public boolean setPlayer(EntityPlayer player){//sets a player, return true if it was added to the game
    if(players[0]==null){
        players[0] = player.username;
        return true;
    }else if(players[1]==null){
        players[1] = player.username;
        return true;
    }
    return false;
}

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



×
×
  • Create New...

Important Information

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