Jump to content

Recommended Posts

Posted

Hi all,

 

I have two GUIs, one that extends GuiContainer to handle trade in my mod, and one with some user help that extends GuiScreen directly. There's a help button on the trade one that opens the other one, using Minecraft.getMinecraft().displayGuiScreen().

 

It works fine, except that opening the help closes the other one. I'd prefer it if the player was taken back to the other one instead. Is this possible "natively", or do I need to do it manually by reopening the first GUI with whatever context it had?

 

Thanks

Posted
  On 11/1/2018 at 2:02 PM, Kinniken said:

I can reuse the same GuiScreen object? Convenient, cool.

 

Is there any callback for when a GUI is closed? I can't seem to find any at least for when the GUI is closed by using the escape key. The only solution I can see would be to override keyTyped() and reopen the older gui if I detect that the key pressed was escape, but that feels very hacky...

Expand  

GuiScreen#onGuiClosed() is probably what you want here.  It's actually called whenever the current GUI object (if already non-null) changes, which is generally by Minecraft#displayGuiScreen() - note that a change from non-null to null also counts here.

 

So when you create your sub-GUI, store a reference to Minecraft.currentScreen in your sub-GUI constructor, and in your sub-GUI's onGuiClosed() override, call Minecraft#displayGuiScreen(parentGuiReference) to redisplay the parent GUI.  Note that you're not actually reusing the GuiScreen object - the sub-GUI is a separate object which exists at the same time as the parent GUI.

 

If you need to pass data from the sub-GUI back to the parent GUI, store a reference to the sub-GUI in the parent when you create it (most likely in actionPerformed() when the player clicks a button).  The parent's initGui() method should check for a non-null sub-GUI field and use that to extract any data that was entered by the player.  The important point being that the parent GUI object still exists while the sub-GUI is being shown (because the sub-GUI holds a reference to it), so you're redisplaying an existing GUI object, not a new one.  And the sub-GUI object still exists after it's closed (at least until the parent closes), because the parent GUI holds a reference to it.

 

  • Thanks 1
Posted

onGuiClosed() looked like what I wanted... except that it's called from within displayGuiScreen() before the new GUI is set, so if I call displayGuiScreen() from within it my resetting of currentScreen gets overiden by the parent call.

 

I don"t like the solution much but I went with putting my code in an overriden keyTyped(), that works fine.

 

Thanks for the help.

Posted

Oh and on this:

"Note that you're not actually reusing the GuiScreen object - the sub-GUI is a separate object which exists at the same time as the parent GUI." 

 

Sorry, I wasn't clear, I meant that I'm reusing the GuiScreen object from my original parent screen display to display it again once the second screen is closed. Between the parent screen and the child one it's not possible of course, they are not even instances of the same class in my case.

Posted (edited)
  On 11/1/2018 at 4:58 PM, Kinniken said:

onGuiClosed() looked like what I wanted... except that it's called from within displayGuiScreen() before the new GUI is set, so if I call displayGuiScreen() from within it my resetting of currentScreen gets overiden by the parent call.

 

I don"t like the solution much but I went with putting my code in an overriden keyTyped(), that works fine.

 

Thanks for the help.

Expand  

Actually, looking at the GUI code from PneumaticCraft, that's what we're doing too :)  You're right that it's not very elegant, but it does work.

 

  On 11/1/2018 at 4:59 PM, Kinniken said:

 

Sorry, I wasn't clear, I meant that I'm reusing the GuiScreen object from my original parent screen display to display it again once the second screen is closed. Between the parent screen and the child one it's not possible of course, they are not even instances of the same class in my case.

Expand  

Right, yes - that's perfectly OK to do.  You can keep a GUI object around for a while by holding a reference to it even though it's not the current GUI.

Edited by desht

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

    • You are using Create 6 - some addons are not compatible with it Remove all addons and add these one by one littlecontraptions is mentioned - keep this one removed
    • Different problem now. https://paste.ee/p/iDo8lS35
    • I would like to have a BoP sapling drop from my block if it is also installed. I think I have done everything and I cannot pinpoint the problem, which is the error in the logs that appears when joining a world:   [Worker-Main-11/ERROR] [ne.mi.co.ForgeHooks/]: Couldn't parse element loot_tables:grasses:blocks/leaves_block com.google.gson.JsonSyntaxException: Expected name to be an item, was unknown string 'biomesoplenty:magic_sapling' My code:   LootItemConditions.CONDITIONS.register(modEventBus); public class LootItemConditions { public static final DeferredRegister<LootItemConditionType> CONDITIONS = DeferredRegister.create(Registries.LOOT_CONDITION_TYPE, Grasses.MOD_ID); public static final RegistryObject<LootItemConditionType> IS_MOD_LOADED = CONDITIONS.register("is_mod_loaded", () -> new LootItemConditionType(new IsModLoaded.ConditionSerializer())); } public class IsModLoaded implements LootItemCondition { private final boolean exists; private final String modID; public IsModLoaded(String modID) { this.exists = ModList.get().isLoaded(modID); this.modID = modID; } @Override public LootItemConditionType getType() { return LootItemConditions.IS_MOD_LOADED.get(); } @Override public boolean test(LootContext context) { return this.exists; } public static LootItemCondition.Builder builder(String modid) { return () -> new IsModLoaded(modid); } public static class ConditionSerializer implements Serializer<IsModLoaded> { @Override public void serialize(JsonObject json, IsModLoaded instance, JsonSerializationContext ctx) { json.addProperty("modid", instance.modID); } @Override public IsModLoaded deserialize(JsonObject json, JsonDeserializationContext ctx) { return new IsModLoaded(GsonHelper.getAsString(json, "modid")); } } } protected LootTable.Builder createLeavesDropsWithModIDCheck(Block selfBlock, Item sapling, Property<?>[] properties, String modIDToCheck, float... chances) { CopyBlockState.Builder blockStateCopyBuilder = CopyBlockState.copyState(selfBlock); for(Property<?> property : properties) { blockStateCopyBuilder.copy(property); } return LootTable.lootTable() .withPool(LootPool.lootPool().setRolls(ConstantValue.exactly(1.0F)) .add(LootItem.lootTableItem(selfBlock) .when(HAS_SHEARS_OR_SILK_TOUCH) .apply(blockStateCopyBuilder))) .withPool(LootPool.lootPool().setRolls(ConstantValue.exactly(1.0F)) .add(this.applyExplosionCondition(selfBlock, LootItem.lootTableItem(sapling)) .when(IsModLoaded.builder(modIDToCheck))) .when(BonusLevelTableCondition.bonusLevelFlatChance(Enchantments.BLOCK_FORTUNE, chances)) .when(HAS_NO_SHEARS_OR_SILK_TOUCH)) .withPool(LootPool.lootPool().name("sticks").setRolls(ConstantValue.exactly(1.0F)) .add(this.applyExplosionDecay(selfBlock, LootItem.lootTableItem(Items.STICK). apply(SetItemCountFunction.setCount(UniformGenerator.between(1.0F, 2.0F)))) .when(BonusLevelTableCondition.bonusLevelFlatChance(Enchantments.BLOCK_FORTUNE, NORMAL_LEAVES_STICK_CHANCES)) .when(HAS_NO_SHEARS_OR_SILK_TOUCH))); } I don't know. Am I making a mistake somewhere? Am I forgetting something? Should there be something else?
  • Topics

×
×
  • Create New...

Important Information

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