From 441b625769d575583677e527e13b89dcf1c1b807 Mon Sep 17 00:00:00 2001 From: khjxiaogu <54445543+khjxiaogu@users.noreply.github.com> Date: Tue, 28 Jan 2025 13:37:04 +0800 Subject: [PATCH] add comment --- .../dataholders/team/CTeamDataManager.java | 12 +- .../com/teammoeg/chorda/menu/CBaseMenu.java | 163 +++++++++++++++--- .../chorda/menu/CBlockEntityMenu.java | 7 + .../teammoeg/chorda/menu/CCustomMenuSlot.java | 1 - .../generator/GeneratorContainer.java | 13 +- .../frostedheart/content/town/TeamTown.java | 9 - 6 files changed, 155 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/teammoeg/chorda/dataholders/team/CTeamDataManager.java b/src/main/java/com/teammoeg/chorda/dataholders/team/CTeamDataManager.java index 5ca01ab08..1774ce73f 100644 --- a/src/main/java/com/teammoeg/chorda/dataholders/team/CTeamDataManager.java +++ b/src/main/java/com/teammoeg/chorda/dataholders/team/CTeamDataManager.java @@ -43,8 +43,6 @@ import com.teammoeg.chorda.events.TeamLoadedEvent; import com.teammoeg.chorda.util.struct.OptionalLazy; -import org.apache.commons.io.FileUtils; - import com.mojang.authlib.GameProfile; import dev.ftb.mods.ftbteams.api.FTBTeamsAPI; @@ -73,6 +71,8 @@ public class CTeamDataManager { public static final LevelResource dataFolder = new LevelResource("chorda_data"); private Map dataByFTBId = new HashMap<>(); private Map dataByOwnId = new HashMap<>(); + private Map playerOwnedTeam; + private Map playerNameOwnedTeam; public static RecipeManager getRecipeManager() { if (getServer() != null) @@ -113,14 +113,6 @@ public void forAllData(SpecialDataType type,BiConsume } - /** - * Helper method to get the data from a team. - * @param team the team - * @return data - */ - public static TeamDataHolder getDataByTeam(Team team) { - return INSTANCE.get(team); - } /** * Helper method to get the data from frostedheart team id. diff --git a/src/main/java/com/teammoeg/chorda/menu/CBaseMenu.java b/src/main/java/com/teammoeg/chorda/menu/CBaseMenu.java index dadafb9f2..4bcd7d492 100644 --- a/src/main/java/com/teammoeg/chorda/menu/CBaseMenu.java +++ b/src/main/java/com/teammoeg/chorda/menu/CBaseMenu.java @@ -24,6 +24,9 @@ import java.util.function.BooleanSupplier; import java.util.function.Function; import java.util.function.Predicate; +import java.util.function.Supplier; + +import javax.annotation.Nonnull; import com.teammoeg.chorda.ChordaNetwork; import com.teammoeg.chorda.menu.CCustomMenuSlot.SyncableDataSlot; @@ -33,6 +36,7 @@ import blusunrize.immersiveengineering.common.gui.IEContainerMenu.MoveItemsFunc; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; @@ -41,30 +45,109 @@ import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.Slot; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.util.Lazy; public abstract class CBaseMenu extends AbstractContainerMenu { public static class Validator implements Predicate { - Vec3 initial; - int distsqr; - - public Validator(BlockPos initial, int distance) { + private static record RadiusCheck(Vec3 initial,float distsqr) implements Predicate { + @Override + public boolean test(Player t) { + return t.position().distanceToSqr(initial) <= distsqr; + } + }; + private static record DynamicRadiusCheck(Supplier initial,float distsqr) implements Predicate { + @Override + public boolean test(Player t) { + return t.position().distanceToSqr(initial.get()) <= distsqr; + } + }; + private static record LevelCheck(Supplier initial) implements Predicate { + @Override + public boolean test(Player t) { + return initial.get()==t.level(); + } + }; + private static record BlockEntityCheck(BlockEntity be) implements Predicate { + @Override + public boolean test(Player t) { + return !be.isRemoved(); + } + }; + private static record EntityCheck(Entity e) implements Predicate { + @Override + public boolean test(Player t) { + return !e.isRemoved(); + } + }; + List> citeria=new ArrayList<>(); + public Validator() { super(); - this.initial = Vec3.atCenterOf(initial); - this.distsqr = distance * distance; + } + public Validator range(BlockPos center,float radius) { + citeria.add(new RadiusCheck(Vec3.atCenterOf(center),radius*radius)); + return this; + } + public Validator range(Supplier center,float radius) { + citeria.add(new DynamicRadiusCheck(center,radius*radius)); + return this; } + public Validator level(Supplier level) { + citeria.add(new LevelCheck(level)); + return this; + } + public Validator level(Level level) { + citeria.add(new LevelCheck(()->level)); + return this; + } + public Validator blockEntity(BlockEntity block,float radius) { + blockEntityWithoutRange(block); + level(block::getLevel); + return range(block.getBlockPos(),radius); + } + public Validator blockEntity(BlockEntity block) { + return blockEntity(block,8); + } + public Validator blockEntityWithoutRange(BlockEntity block) { + citeria.add(new BlockEntityCheck(block)); + return this; + } + public Validator entity(Entity entity,float radius) { + entityWithoutRange(entity); + level(entity::level); + range(entity::position,radius); + return this; + } + public Validator entity(Entity block) { + return entity(block,8); + } + public Validator entityWithoutRange(Entity entity) { + citeria.add(new EntityCheck(entity)); + return this; + } + public Validator custom(Predicate predicate) { + citeria.add(predicate); + return this; + } + public Validator custom(BooleanSupplier predicate) { + citeria.add(p->predicate.getAsBoolean()); + return this; + } @Override public boolean test(Player t) { - return t.position().distanceToSqr(initial) <= distsqr; - } - - public Predicate and(BooleanSupplier supp) { - return this.and(t -> supp.getAsBoolean()); + for(Predicate p:citeria) { + if(!p.test(t))return false; + } + return true; } } - + /** + * A simple builder to define the slot fill order when player shift-click its invertory slot + * It would start to fill from the earlier defined range, then the later + * */ public static class QuickMoveStackBuilder { private static record Range(int start, int end, boolean reverse) { private Range(int slot) { @@ -77,29 +160,45 @@ private Range(int slot) { private QuickMoveStackBuilder() { } - + /** + * Define an empty behaviour + * */ public static QuickMoveStackBuilder begin() { return new QuickMoveStackBuilder(); } - + /** + * Define a single slot to fill + * */ public static QuickMoveStackBuilder first(int slot) { return begin().then(slot); } - + /** + * Define a range of slot to fill + * + * */ public static QuickMoveStackBuilder first(int beginInclusive, int endExclusive) { return begin().then(beginInclusive, endExclusive); } - + /** + * Define a single slot to fill + * */ public QuickMoveStackBuilder then(int slot) { ranges.add(new Range(slot)); return this; } - + /** + * Define a range of slot to fill + * + * */ public QuickMoveStackBuilder then(int beginInclusive, int endExclusive) { ranges.add(new Range(beginInclusive, endExclusive, false)); return this; } - + /** + * Define a range of slot to fill + * + * @param reversed to define whether later slot to be filled first + * */ public QuickMoveStackBuilder then(int beginInclusive, int endExclusive, boolean reversed) { ranges.add(new Range(beginInclusive, endExclusive, reversed)); return this; @@ -120,29 +219,41 @@ public Function build(CBaseMenu t) { protected final int INV_START; protected static final int INV_SIZE = 36; protected static final int INV_QUICK = 27; - protected Predicate validator; + private final Lazy validator=Lazy.of(()->buildValidator(new Validator())); protected Lazy> moveFunction = Lazy.of(() -> defineQuickMoveStack().build(this)); protected List> specialDataSlots = new ArrayList<>(); private Player player; - + /** + * Constructor of c base menu + * Implementation notes: + * You can call {@link #addPlayerInventory addPlayerInventory} to add defaulted player inventory slots + * You may override {@link #buildValidator(Validator) buildValidator} to define rules whether this menu should close + * You may override {@link #defineQuickMoveStack() defineQuickMoveStack} to define quickMoveStack(shift-click) behaviour of the menu slots,default ones is to fill from the first slot to the last one. + * You may use {@link CCustomMenuSlot} to broadcast ui changes to the client menu. + * @param inv_start start slot index of player inventory, for example, if the machine has 5 slots, then this should be 5 + * + * */ public CBaseMenu(MenuType pMenuType, int pContainerId, Player player, int inv_start) { super(pMenuType, pContainerId); this.INV_START = inv_start; this.player = player; } - protected void addPlayerInventory(Inventory inv, int dx, int dy, int quickBarY) { + protected void addPlayerInventory(Inventory inv, int invX, int invY, int quickBarY) { for (int i = 0; i < 3; i++) for (int j = 0; j < 9; j++) - addSlot(new Slot(inv, j + i * 9 + 9, dx + j * 18, dy + i * 18)); + addSlot(new Slot(inv, j + i * 9 + 9, invX + j * 18, invY + i * 18)); for (int i = 0; i < 9; i++) - addSlot(new Slot(inv, i, dx + i * 18, quickBarY)); + addSlot(new Slot(inv, i, invX + i * 18, quickBarY)); } public QuickMoveStackBuilder defineQuickMoveStack() { return QuickMoveStackBuilder.first(0, INV_START); } - + @Nonnull + protected Validator buildValidator(@Nonnull Validator builder) { + return builder; + } public boolean quickMoveIn(ItemStack slotStack) { return moveFunction.get().apply(slotStack); } @@ -324,9 +435,7 @@ public void broadcastChanges() { @Override public boolean stillValid(Player pPlayer) { - if (validator == null) - return true; - return validator.test(pPlayer); + return validator.get().test(pPlayer); } } \ No newline at end of file diff --git a/src/main/java/com/teammoeg/chorda/menu/CBlockEntityMenu.java b/src/main/java/com/teammoeg/chorda/menu/CBlockEntityMenu.java index b88c8fd7e..6c5a86359 100644 --- a/src/main/java/com/teammoeg/chorda/menu/CBlockEntityMenu.java +++ b/src/main/java/com/teammoeg/chorda/menu/CBlockEntityMenu.java @@ -19,6 +19,8 @@ package com.teammoeg.chorda.menu; +import com.teammoeg.chorda.menu.CBaseMenu.Validator; + import blusunrize.immersiveengineering.common.gui.BlockEntityInventory; import blusunrize.immersiveengineering.common.util.inventory.IIEInventory; import net.minecraft.world.Container; @@ -44,6 +46,11 @@ else if (blockEntity instanceof Container cont) } + @Override + protected Validator buildValidator(Validator builder) { + return super.buildValidator(builder).blockEntity(blockEntity); + } + @Override public boolean stillValid(Player pPlayer) { return !blockEntity.isRemoved(); diff --git a/src/main/java/com/teammoeg/chorda/menu/CCustomMenuSlot.java b/src/main/java/com/teammoeg/chorda/menu/CCustomMenuSlot.java index 6262c028c..ea3cd181f 100644 --- a/src/main/java/com/teammoeg/chorda/menu/CCustomMenuSlot.java +++ b/src/main/java/com/teammoeg/chorda/menu/CCustomMenuSlot.java @@ -39,7 +39,6 @@ import net.minecraft.world.inventory.DataSlot; import net.minecraftforge.fluids.FluidStack; /** - * CContainerData * a utility class for menu data sync and type conversion * * */ diff --git a/src/main/java/com/teammoeg/frostedheart/content/climate/heatdevice/generator/GeneratorContainer.java b/src/main/java/com/teammoeg/frostedheart/content/climate/heatdevice/generator/GeneratorContainer.java index bb70d26f2..61c761107 100644 --- a/src/main/java/com/teammoeg/frostedheart/content/climate/heatdevice/generator/GeneratorContainer.java +++ b/src/main/java/com/teammoeg/frostedheart/content/climate/heatdevice/generator/GeneratorContainer.java @@ -23,6 +23,7 @@ import com.teammoeg.chorda.client.ui.Point; import com.teammoeg.chorda.menu.CBaseMenu; +import com.teammoeg.chorda.menu.CBaseMenu.Validator; import com.teammoeg.chorda.menu.CCustomMenuSlot; import com.teammoeg.chorda.menu.CCustomMenuSlot.CDataSlot; import com.teammoeg.chorda.multiblock.CMultiblockHelper; @@ -51,7 +52,7 @@ public abstract class GeneratorContainer isWorking = CCustomMenuSlot.SLOT_BOOL.create(this); public CDataSlot isOverdrive = CCustomMenuSlot.SLOT_BOOL.create(this); public CDataSlot pos = CCustomMenuSlot.SLOT_BLOCKPOS.create(this); - + MultiblockMenuContext ctx; public GeneratorContainer(MenuType type, int id, Inventory inventoryPlayer, MultiblockMenuContext ctx) { super(type, id, inventoryPlayer.player, 2); R state = ctx.mbContext().getState(); @@ -77,13 +78,19 @@ public GeneratorContainer(MenuType type, int id, Inventory inventoryPlayer, M //System.out.println(" binded "); }); //System.out.println(optdata); + this.ctx=ctx; pos.bind(() -> ctx.clickedPos()); - this.validator = new Validator(ctx.clickedPos(), 8).and(ctx.mbContext().isValid()); + IItemHandler handler = state.getData(CMultiblockHelper.getAbsoluteMaster(ctx.mbContext())).map(t -> t.inventory).orElseGet(() -> null); createSlots(handler, inventoryPlayer); } - public GeneratorContainer(MenuType type, int id, Inventory inventoryPlayer) { + @Override + protected Validator buildValidator(Validator builder) { + return super.buildValidator(builder).range(ctx.clickedPos(),8).custom(ctx.mbContext().isValid()); + } + + public GeneratorContainer(MenuType type, int id, Inventory inventoryPlayer) { super(type, id, inventoryPlayer.player, 2); createSlots(new ItemStackHandler(2), inventoryPlayer); } diff --git a/src/main/java/com/teammoeg/frostedheart/content/town/TeamTown.java b/src/main/java/com/teammoeg/frostedheart/content/town/TeamTown.java index a2a9c68c5..9934732dd 100644 --- a/src/main/java/com/teammoeg/frostedheart/content/town/TeamTown.java +++ b/src/main/java/com/teammoeg/frostedheart/content/town/TeamTown.java @@ -64,15 +64,6 @@ public static TeamTown from(Player player) { return new TeamTown(data); } - /** - * Get the town for a team. - * @param team the team - * @return the town - */ - public static TeamTown from(Team team) { - TeamTownData data = CTeamDataManager.getDataByTeam(team).getData(FHSpecialDataTypes.TOWN_DATA); - return new TeamTown(data); - } /** * Default constructor links storage to the town data.