Skip to content

Commit

Permalink
add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Jan 28, 2025
1 parent 3462eaf commit 441b625
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +71,8 @@ public class CTeamDataManager {
public static final LevelResource dataFolder = new LevelResource("chorda_data");
private Map<UUID, UUID> dataByFTBId = new HashMap<>();
private Map<UUID, TeamDataHolder> dataByOwnId = new HashMap<>();
private Map<UUID, UUID> playerOwnedTeam;
private Map<String,UUID> playerNameOwnedTeam;

public static RecipeManager getRecipeManager() {
if (getServer() != null)
Expand Down Expand Up @@ -113,14 +113,6 @@ public <T extends SpecialData> void forAllData(SpecialDataType<T> 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.
Expand Down
163 changes: 136 additions & 27 deletions src/main/java/com/teammoeg/chorda/menu/CBaseMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<Player> {
Vec3 initial;
int distsqr;

public Validator(BlockPos initial, int distance) {
private static record RadiusCheck(Vec3 initial,float distsqr) implements Predicate<Player> {
@Override
public boolean test(Player t) {
return t.position().distanceToSqr(initial) <= distsqr;
}
};
private static record DynamicRadiusCheck(Supplier<Vec3> initial,float distsqr) implements Predicate<Player> {
@Override
public boolean test(Player t) {
return t.position().distanceToSqr(initial.get()) <= distsqr;
}
};
private static record LevelCheck(Supplier<Level> initial) implements Predicate<Player> {
@Override
public boolean test(Player t) {
return initial.get()==t.level();
}
};
private static record BlockEntityCheck(BlockEntity be) implements Predicate<Player> {
@Override
public boolean test(Player t) {
return !be.isRemoved();
}
};
private static record EntityCheck(Entity e) implements Predicate<Player> {
@Override
public boolean test(Player t) {
return !e.isRemoved();
}
};
List<Predicate<Player>> 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<Vec3> center,float radius) {
citeria.add(new DynamicRadiusCheck(center,radius*radius));
return this;
}

public Validator level(Supplier<Level> 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<Player> 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<Player> and(BooleanSupplier supp) {
return this.and(t -> supp.getAsBoolean());
for(Predicate<Player> 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) {
Expand All @@ -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;
Expand All @@ -120,29 +219,41 @@ public Function<ItemStack, Boolean> build(CBaseMenu t) {
protected final int INV_START;
protected static final int INV_SIZE = 36;
protected static final int INV_QUICK = 27;
protected Predicate<Player> validator;
private final Lazy<Validator> validator=Lazy.of(()->buildValidator(new Validator()));
protected Lazy<Function<ItemStack, Boolean>> moveFunction = Lazy.of(() -> defineQuickMoveStack().build(this));
protected List<SyncableDataSlot<?>> 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);
}
Expand Down Expand Up @@ -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);
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/teammoeg/chorda/menu/CBlockEntityMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +52,7 @@ public abstract class GeneratorContainer<R extends GeneratorState, T extends Gen
public CDataSlot<Boolean> isWorking = CCustomMenuSlot.SLOT_BOOL.create(this);
public CDataSlot<Boolean> isOverdrive = CCustomMenuSlot.SLOT_BOOL.create(this);
public CDataSlot<BlockPos> pos = CCustomMenuSlot.SLOT_BLOCKPOS.create(this);

MultiblockMenuContext<R> ctx;
public GeneratorContainer(MenuType<?> type, int id, Inventory inventoryPlayer, MultiblockMenuContext<R> ctx) {
super(type, id, inventoryPlayer.player, 2);
R state = ctx.mbContext().getState();
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 441b625

Please sign in to comment.