Jump to content

[1.12.2] General question about world "events"


Recommended Posts

Posted

What would be the best way to create an event that happens in the world. Like on the push of a button in a gui, cause a system of actions to occur for the next say ten seconds? Would i be easier to do this in the same tileentity or can i do it with a separate ticking entity?

The seven became one and the one became two.

Posted
15 minutes ago, Merthew said:

Like on the push of a button in a gui, cause a system of actions to occur for the next say ten seconds?

Depends. If you do not care whether the execution finishes or not then you can do it in your TE. However if you do care then you need to do it in a different way since a TE can get unloaded. Something like this(pseudo-code):

class Gui
{
    ...
    void onButtonPress(GuiButton btn)
    {
        Network.sendMessageToServer(new MessageDoThings());
    }
    ...
}

class MessageDoThingsHandler implements IMessageHandler<MessageDoThings, IMessage>
{
    ...
    IMessage handleMessage(MessageDoThings message, IContext context)
    {
        EntityPlayerMP sender = cotext.getSender();
        World world = sender.world;
        IWorldCap worldCap = world.getCapability(YOUR_CAP, null);
        worldCap.addAction(new DoThings(YOUR_TIMER));
    }
}

class WorldCap implements IWorldCap
{
    List<IWorldAction> actions = Lists.newArrayList();
    
    @Override
    void addAction(IWorldAction action)
    {
        this.actions.add(action);
    }
    
    @Override
    void onTick()
    {
        for (int i = this.actions.size() - 1; i >= 0; --i)
        {
            IWorldAction act = this.actions.get(i);
            if (!act.run())
            {
                this.actions.remove(act);
            }
        }
    }
}

interface IWorldAction
{
    boolean run();
}

class DoThings implements IWorldAction
{
    final int timer = 0;
    
    DoThings(int i)
    {
        this.timer = i;
    }
    
    @Override
    boolean run()
    {
        if (this.timer % 20 == 0)
        {
            this.doThings();
        }
        
        return this.timer-- == 0;
    }
    
    void doThings()
    {
        ...
    }
}

@EventBusSubscriber
class WorldHandler
{
    @SubscribeEvent
    static void onWorldTick(WorldTickEvent event)
    {
        if (event.phase != Phase.END)
        {
            event.world.getCapability(YOUR_CAP, null).onTick();
        }
    }
}

 

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.