Skip to content

Commit

Permalink
feat: openSoon
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Nov 11, 2024
1 parent 797019e commit b578735
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ kotlin_version=2.0.21
fabric_kotlin_version=1.12.3

# mod properties
mod_version=1.3.6
mod_version=1.3.7
maven_group=net.mcbrawls
mod_id=slate
4 changes: 4 additions & 0 deletions src/main/java/net/mcbrawls/slate/SlatePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ default SlateScreenHandler<?> getSlateScreenHandler() {
}

default boolean hasSlateOpen() {
return this.getSlateScreenHandler() != null;
}

default void setSoonSlate(Slate slate) {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public abstract class ServerPlayerEntityMixin extends PlayerEntity implements Sl
@Unique
private boolean ignoreNextClosePacket = false;

@Unique
private Slate soonSlate = null;

private ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile) {
super(world, pos, yaw, gameProfile);
}
Expand Down Expand Up @@ -69,6 +72,18 @@ private void closeSlateOnDeath(DamageSource source, CallbackInfo ci) {
}
}

@Inject(method = "playerTick", at = @At("TAIL"))
private void onTickEnd(CallbackInfo ci) {
if (this.soonSlate != null) {
if (!hasSlateOpen()) {
ServerPlayerEntity that = (ServerPlayerEntity) (Object) this;
this.soonSlate.open(that);
}

this.soonSlate = null;
}
}

@Override
public boolean openSlate(Slate slate) {
return slate.open((ServerPlayerEntity) (Object) this);
Expand All @@ -90,7 +105,7 @@ public boolean openSlate(Slate slate) {
}

@Override
public boolean hasSlateOpen() {
return this.getSlateScreenHandler() != null;
public void setSoonSlate(Slate slate) {
this.soonSlate = slate;
}
}
22 changes: 18 additions & 4 deletions src/main/kotlin/net/mcbrawls/slate/Slate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ open class Slate {
* @return whether the slate was opened successfully
*/
open fun open(player: ServerPlayerEntity): Boolean {
handledSlate?.also { handledSlate ->
if (player != handledSlate.player) {
logger.warn("Tried to reopen already opened slate for different player: $this, $handledSlate")
if (handledSlate != null) {
if (player != handledSlate?.player) {
logger.debug("Tried to reopen already opened slate for different player: {}, {}", this, handledSlate)
} else {
logger.warn("Tried to reopen already opened slate: $this, $handledSlate")
logger.debug("Tried to reopen already opened slate: {}, {}", this, handledSlate)
}

return false
Expand All @@ -256,6 +256,20 @@ open class Slate {
return false
}

/**
* Opens this slate at the end of the current tick.
*/
fun openSoon(player: ServerPlayerEntity): Boolean {
if (handledSlate != null) {
return false
}

val slatePlayer = player as SlatePlayer
slatePlayer.setSoonSlate(this)

return true
}

/**
* Opens the handled screen for this player.
* @return an optional sync id integer
Expand Down
9 changes: 6 additions & 3 deletions src/test/kotlin/net/mcbrawls/slate/test/SlateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class SlateTest : ModInitializer {

callbacks {
onInput { _, _, input ->
println(input)
println("Input: $input")
}
}
}
Expand Down Expand Up @@ -279,8 +279,11 @@ class SlateTest : ModInitializer {
println("Closed")
}

onChildClose { slate, player ->
println("Child closed: $slate")
onChildClose { childSlate, player ->
println("Child closed: $childSlate")

// ensure that the parent is always opened
openSoon(player)
}
}
}
Expand Down

0 comments on commit b578735

Please sign in to comment.