Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some planned screen anvancement and let functions of screen support crafter. #1833

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/carpet/mixins/ServerPlayer_scarpetEventMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import carpet.fakes.EntityInterface;
import carpet.fakes.ServerPlayerInterface;
import carpet.script.CarpetEventServer;
import carpet.script.EntityEventsGroup;
import com.mojang.authlib.GameProfile;
import net.minecraft.core.BlockPos;
Expand All @@ -10,6 +11,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stat;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -45,6 +47,14 @@ public ServerPlayer_scarpetEventMixin(Level level, BlockPos blockPos, float f, G

@Shadow public boolean wonGame;

@Inject(method = "openMenu", at = @At("RETURN"))
private void grabStat(MenuProvider menuProvider, CallbackInfoReturnable<java.util.OptionalInt> cir)
{
if (cir.getReturnValue().isPresent()) {
CarpetEventServer.Event.PLAYER_OPEN_SCREEN.onPlayerEvent((ServerPlayer)(Object)this);
};
}

@Redirect(method = "completeUsingItem", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/player/Player;completeUsingItem()V"
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/carpet/script/CarpetEventServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,17 @@ public void onSlotSwitch(ServerPlayer player, int from, int to)
), player::createCommandSourceStack);
}
};
public static final Event PLAYER_OPEN_SCREEN = new Event("player_open_screen", 1, false)
{
@Override
public boolean onPlayerEvent(ServerPlayer player)
{
return handler.call(() ->
Arrays.asList(
new EntityValue(player)
), player::createCommandSourceStack);
}
};
public static final Event PLAYER_SWAPS_HANDS = new Event("player_swaps_hands", 1, false)
{
@Override
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/carpet/script/api/Inventories.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,27 @@ else if (owner instanceof LivingEntity livingEntity)
return new ScreenValue(player, type, name, function, c);
});

expression.addContextFunction("get_current_screen", -1, (c, t, lv) ->
{
if (lv.size() < 1)
{
throw new InternalExpressionException("'get_current_screen' requires at least 1 argument");
}
Value playerValue = lv.get(0);
ServerPlayer player = EntityValue.getPlayerByValue(((CarpetContext) c).server(), playerValue);
if (player == null)
{
throw new InternalExpressionException("'get_current_screen' requires a valid online player as the first argument.");
}
FunctionValue function = null;
if (lv.size() > 1)
{
function = FunctionArgument.findIn(c, expression.module, lv, 1, true, false).function;
}

return new ScreenValue(player, function, c);
});

expression.addContextFunction("close_screen", 1, (c, t, lv) ->
{
Value value = lv.get(0);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/carpet/script/value/ScreenValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.OptionalInt;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
Expand Down Expand Up @@ -130,6 +131,26 @@ public ScreenValue(ServerPlayer player, String type, Component name, @Nullable F
this.inventory = new ScreenHandlerInventory(this.screenHandler);
}

public ScreenValue(ServerPlayer player, @Nullable FunctionValue callback, Context c)
{
this.screenHandler = player.containerMenu;

this.name = Component.literal("seems that the game forgot that");//should i make something like a weak map to remember it?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not use a map, since you aren't gonna know whether a screen will ever be retrieved from get_current_screen at all. It just creates additional overhead, that would be required even if no scripts are running. Setting it to null and returning a null value when querying name is probably the best option IMO.

Copy link
Contributor Author

@17183248569 17183248569 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. i let it be null now.

this.typestring = player.hasContainerOpen()? ValueConversions.simplify(BuiltInRegistries.MENU.getKey(screenHandler.getType())):"inventory";

if (callback != null)
{
callback.checkArgs(4);
}
this.callback = callback;
this.hostname = c.host.getName();
this.scriptServer = (CarpetScriptServer) c.host.scriptServer();
this.player = player;

addListenerCallback(screenHandler);
this.inventory = new ScreenHandlerInventory(this.screenHandler);
}

private MenuProvider createScreenHandlerFactory()
{
if (!screenHandlerFactories.containsKey(this.typestring))
Expand Down