Skip to content

Commit

Permalink
improvements to routes setting gui
Browse files Browse the repository at this point in the history
the gui now properly updates when changing values
  • Loading branch information
Thutmose committed Oct 11, 2022
1 parent abdfd04 commit d679b54
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 18 deletions.
14 changes: 0 additions & 14 deletions src/main/java/pokecube/adventures/PokecubeAdv.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
Expand Down Expand Up @@ -105,17 +102,6 @@ public static void onEntityAttributes(final EntityAttributeCreationEvent event)
event.put(EntityTypes.getTrainer(), attribs.build());
event.put(EntityTypes.getLeader(), attribs.build());
}

@OnlyIn(Dist.CLIENT)
@SubscribeEvent
public static void textureStitch(final TextureStitchEvent.Pre event)
{
if (!event.getAtlas().location().toString().equals("minecraft:textures/atlas/blocks.png")) return;
event.addSprite(new ResourceLocation(PokecubeAdv.MODID, "gui/slot_dna"));
event.addSprite(new ResourceLocation(PokecubeAdv.MODID, "gui/slot_egg"));
event.addSprite(new ResourceLocation(PokecubeAdv.MODID, "gui/slot_bottle"));
event.addSprite(new ResourceLocation(PokecubeAdv.MODID, "gui/slot_selector"));
}
}

public static final String MODID = "pokecube_adventures";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.mojang.blaze3d.vertex.PoseStack;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Button.OnPress;
import net.minecraft.client.gui.components.EditBox;
Expand Down Expand Up @@ -34,9 +35,20 @@ public class AI extends Page
EditBox battleCooldown;
EditBox faceDirection;

Runnable callback = () -> {
if (Minecraft.getInstance().screen == this.parent)
{
// Re-add the callback
this.parent.guard.attachChangeListener(this.callback);
// Re-initialise the list
this.onPageOpened();
}
};

public AI(final EditorGui parent)
{
super(TComponent.literal(""), parent);
parent.guard.attachChangeListener(callback);
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/pokecube/core/ai/routes/GuardAICapability.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ protected void setWalkTo(final Mob entity, final Vec3 pos, final double speed, f

private final List<IGuardTask> tasks = Lists.newArrayList(new GuardTask());

private List<Runnable> listeners = Lists.newArrayList();

private GuardState state = GuardState.IDLE;

private IGuardTask activeTask;
Expand All @@ -189,6 +191,20 @@ public void setTask(final int index, final IGuardTask task)
IGuardAICapability.super.setTask(index, task);
}

@Override
public void attachChangeListener(Runnable onChanged)
{
this.listeners.add(onChanged);
}

@Override
public void onChanged()
{
List<Runnable> dirty = Lists.newArrayList(listeners);
this.listeners.clear();
dirty.forEach(r -> r.run());
}

@Override
public GuardState getState()
{
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/pokecube/core/ai/routes/IGuardAICapability.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ default void setTask(final int index, final IGuardTask task)
this.getTasks().set(index, task);
}

default void attachChangeListener(Runnable onChanged)
{}

default void onChanged()
{}

// do we have a task with a location, and a position
boolean hasActiveTask(long time, long daylength);

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/pokecube/core/client/gui/helper/GuardEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,10 @@ public void update()
final Component mess = TComponent.translatable("pokemob.route.updated");
this.parent.getMinecraft().player.displayClientMessage(mess, false);
}
else
{
final Component mess = TComponent.translatable("pokecube.route.info.incomplete");
this.parent.getMinecraft().player.displayClientMessage(mess, false);
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/pokecube/core/client/gui/pokemob/tabs/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.common.collect.Lists;
import com.mojang.blaze3d.vertex.PoseStack;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.nbt.CompoundTag;
Expand All @@ -32,11 +33,28 @@ public class Routes extends Tab
List<EditBox> variations = Lists.newArrayList();
int num;

Runnable callback = () -> {
if (Minecraft.getInstance().screen == this.parent)
{
// This does 1 set of disabling.
this.setEnabled(false);
// then we clear
this.clear();
// Then re-initialise
this.init();
// Then re-add the callback
this.guard.attachChangeListener(this.callback);
// and finally re-enable.
this.setEnabled(true);
}
};

public Routes(GuiPokemob parent)
{
super(parent, "routes");
this.entity = this.menu.pokemob.getEntity();
this.guard = this.entity.getCapability(CapHolders.GUARDAI_CAP, null).orElse(null);
this.guard.attachChangeListener(callback);
this.icon = Resources.TAB_ICON_ROUTES;
}

Expand Down Expand Up @@ -75,6 +93,7 @@ public void init()
};
final int dx = 3;
final int dy = 25;

RouteEditHelper.getGuiList(this.list, this.guard, function, this.entity, this.parent, 60, dx, dy, 50);

for (var entry : list.children)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import pokecube.core.ai.routes.IGuardAICapability.IGuardTask;
import pokecube.core.network.pokemobs.PacketPokemobGui;
import pokecube.core.utils.CapHolders;
import thut.core.common.network.EntityUpdate;
import thut.core.common.network.Packet;

public class PacketSyncRoutes extends Packet
{
public static void applyServerPacket(final Tag tag, final Entity mob, final IGuardAICapability guard)
public static void applyServerPacket(final Tag tag, final Entity mob, final IGuardAICapability guard,
ServerPlayer player)
{
final CompoundTag nbt = (CompoundTag) tag;
final int index = nbt.getInt("I");
Expand All @@ -40,7 +40,8 @@ else if (nbt.contains("N"))
guard.getTasks().set(index2, temp);
}
else if (index < guard.getTasks().size()) guard.getTasks().remove(index);
EntityUpdate.sendEntityUpdate(mob);
// Send back a packet to notify of the changes.
sendUpdateClientPacket(mob, player, false);
}

public static void sendServerPacket(final Entity mob, final Tag tag)
Expand Down Expand Up @@ -86,6 +87,9 @@ public void handleClient()
final IGuardAICapability guard = e.getCapability(CapHolders.GUARDAI_CAP, null).orElse(null);
guard.loadTasks((ListTag) data.get("R"));
if (data.getBoolean("O")) PacketSyncRoutes.sendServerPacket(e, null);
else
// Notifies the guis of the updates.
guard.onChanged();
}

@Override
Expand All @@ -103,7 +107,7 @@ public void handleServer(final ServerPlayer player)
{
PacketPokemobGui.sendOpenPacket(e, player, PacketPokemobGui.ROUTES);
}
else PacketSyncRoutes.applyServerPacket(data.get("T"), e, guard);
else PacketSyncRoutes.applyServerPacket(data.get("T"), e, guard, player);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/pokecube/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
"pokecube.route.info.time.formatinfo": "§cError, Wrong format; Correct time format is: (startick endtick)",
"pokecube.route.info.pos.formatinfo": "§cError, Wrong format; Correct location format is: (x y z)",

"pokecube.route.info.incomplete": "§cError, requires all three boxes to be filled out correctly!",

"_comment": "Pokemob Gui",

"pokemob.stance.guard": "Guarding",
Expand Down

0 comments on commit d679b54

Please sign in to comment.