Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 7 additions & 2 deletions src/main/java/com/gregtechceu/gtceu/api/item/IGTTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ default ItemStack getRaw() {
ItemStack stack = new ItemStack(asItem());
stack.set(DataComponents.ATTRIBUTE_MODIFIERS, ItemAttributeModifiers.EMPTY);
stack.set(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY);
stack.set(GTDataComponents.MAX_AOE, AoESymmetrical.ZERO);

stack.remove(DataComponents.MAX_DAMAGE);
stack.remove(DataComponents.DAMAGE);
stack.remove(DataComponents.UNBREAKABLE);
Expand Down Expand Up @@ -737,10 +739,13 @@ default ModularScreen createScreen(PlayerInventoryGuiData<?> data, ModularPanel<
default @Nullable ModularPanel<?> buildUI(PlayerInventoryGuiData<?> data, PanelSyncManager syncManager,
UISettings settings) {
for (var behavior : getToolStats().getBehaviors()) {
if (!(behavior instanceof IToolUIBehavior uiBehavior) ||
!uiBehavior.shouldOpenUI(data.getPlayer(), data.getPlayer().getUsedItemHand())) {
if (!(behavior instanceof IToolUIBehavior<?> uiBehavior)) {
continue;
}
if (!uiBehavior.shouldOpenUI(data.getPlayer(), data.getPlayer().getUsedItemHand())) {
continue;
}

return uiBehavior.buildUI(data, syncManager, settings);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gregtechceu.gtceu.api.item.datacomponents;

import net.minecraft.Util;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.ExtraCodecs;
Expand All @@ -11,62 +12,79 @@
import lombok.*;
import lombok.experimental.Accessors;

public record AoESymmetrical(int maxColumn, int maxRow, int maxLayer, int column, int row, int layer) {

public static final Codec<AoESymmetrical> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("max_column").forGetter(AoESymmetrical::maxColumn),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("max_row").forGetter(AoESymmetrical::maxRow),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("max_layer").forGetter(AoESymmetrical::maxLayer),
import java.util.List;

public record AoESymmetrical(int column, int row, int layer) {

// spotless:off
private static final Codec<AoESymmetrical> ARRAY_CODEC = Codec.INT
.listOf()
.comapFlatMap(list -> Util.fixedSize(list, 3)
.map(l -> new AoESymmetrical(l.get(0), l.get(1), l.get(2))),
aoe -> List.of(aoe.column, aoe.row, aoe.layer)
);
private static final Codec<AoESymmetrical> NAMED_CODEC = RecordCodecBuilder.create(instance -> instance.group(
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("additional_columns").forGetter(AoESymmetrical::column),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("additional_rows").forGetter(AoESymmetrical::row),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("additional_layers").forGetter(AoESymmetrical::layer)
).apply(instance, AoESymmetrical::new));
private static final Codec<AoESymmetrical> LEGACY_CODEC = RecordCodecBuilder.create(instance -> instance.group(
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("column").forGetter(AoESymmetrical::column),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("row").forGetter(AoESymmetrical::row),
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("layer").forGetter(AoESymmetrical::layer))
.apply(instance, AoESymmetrical::new));
ExtraCodecs.NON_NEGATIVE_INT.fieldOf("layer").forGetter(AoESymmetrical::layer)
).apply(instance, AoESymmetrical::new));
public static final Codec<AoESymmetrical> CODEC = Codec.withAlternative(NAMED_CODEC, Codec.withAlternative(ARRAY_CODEC, LEGACY_CODEC));

public static final StreamCodec<ByteBuf, AoESymmetrical> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.VAR_INT, AoESymmetrical::maxColumn,
ByteBufCodecs.VAR_INT, AoESymmetrical::maxRow,
ByteBufCodecs.VAR_INT, AoESymmetrical::maxLayer,
ByteBufCodecs.VAR_INT, AoESymmetrical::column,
ByteBufCodecs.VAR_INT, AoESymmetrical::row,
ByteBufCodecs.VAR_INT, AoESymmetrical::layer,
AoESymmetrical::new);
AoESymmetrical::new
);
// spotless:on

public static final AoESymmetrical ZERO = new AoESymmetrical(0, 0, 0, 0, 0, 0);
public static final AoESymmetrical ZERO = new AoESymmetrical(0, 0, 0);

public boolean isZero() {
return this == ZERO || (this.maxColumn == 0 && this.maxRow == 0 && this.maxLayer == 0);
return this == ZERO || (this.column == 0 && this.row == 0 && this.layer == 0);
}

public static AoESymmetrical of(int column, int row, int layer) {
Preconditions.checkArgument(column >= 0, "Height cannot be negative.");
Preconditions.checkArgument(row >= 0, "Width cannot be negative.");
Preconditions.checkArgument(layer >= 0, "Depth cannot be negative.");
return column == 0 && row == 0 && layer == 0 ? ZERO :
new AoESymmetrical(column, row, layer, column, row, layer);
new AoESymmetrical(column, row, layer);
}

public AoESymmetrical min(AoESymmetrical other) {
if (this.isZero() || other.isZero()) return AoESymmetrical.ZERO;
if (this.equals(other)) return this;

return new AoESymmetrical(Math.min(column, other.column),
Math.min(row, other.row),
Math.min(layer, other.layer));
}

public Mutable toMutable() {
return new Mutable(maxColumn, maxRow, maxLayer, column, row, layer);
public Mutable toMutable(AoESymmetrical max) {
return new Mutable(max.column, max.row, max.layer, this.column, this.row, this.layer);
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
// noinspection PatternVariableHidesField
if (!(o instanceof AoESymmetrical(int maxColumn, int maxRow, int maxLayer, int column, int row, int layer))) {
if (!(o instanceof AoESymmetrical(int column, int row, int layer))) {
return false;
}

return this.maxColumn == maxColumn && this.maxRow == maxRow && this.maxLayer == maxLayer &&
this.column == column && this.row == row && this.layer == layer;
return this.column == column && this.row == row && this.layer == layer;
}

@Override
public int hashCode() {
int result = maxColumn;
result = 31 * result + maxRow;
result = 31 * result + maxLayer;
result = 31 * result + column;
int result = column;
result = 31 * result + row;
result = 31 * result + layer;
return result;
Expand Down Expand Up @@ -126,7 +144,9 @@ public Mutable decreaseLayer() {
}

public AoESymmetrical toImmutable() {
return new AoESymmetrical(maxColumn, maxRow, maxLayer, column, row, layer);
return new AoESymmetrical(Math.clamp(column, 0, maxColumn),
Math.clamp(row, 0, maxRow),
Math.clamp(layer, 0, maxLayer));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public record ToolBehaviors(@Unmodifiable Map<ToolBehaviorType<?>, IToolBehavior

public static final ToolBehaviors EMPTY = new ToolBehaviors(Map.of());
// spotless:off
public static final Codec<Map<ToolBehaviorType<?>, IToolBehavior<?>>> MAP_CODEC = Codec
private static final Codec<Map<ToolBehaviorType<?>, IToolBehavior<?>>> MAP_CODEC = Codec
.dispatchedMap(GTRegistries.TOOL_BEHAVIORS.byNameCodec(), ToolBehaviorType::getCodec);
public static final Codec<ToolBehaviors> CODEC = MAP_CODEC.xmap(ToolBehaviors::new, ToolBehaviors::behaviors);

Expand All @@ -41,6 +41,14 @@ public ToolBehaviors(List<IToolBehavior<?>> behaviors) {
this(behaviors.stream().collect(Collectors.toMap(IToolBehavior::getType, Function.identity())));
}

public ToolBehaviors(Map<ToolBehaviorType<?>, IToolBehavior<?>> behaviors) {
this.behaviors = Collections.unmodifiableMap(behaviors);
}

public boolean isEmpty() {
return this.behaviors.isEmpty();
}

public boolean hasBehavior(ToolBehaviorType<?> type) {
return behaviors.containsKey(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ public static ToolBehaviors getBehaviorsComponent(ItemStack stack) {
return stack.getOrDefault(GTDataComponents.TOOL_BEHAVIORS, ToolBehaviors.EMPTY);
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static boolean hasBehaviorsComponent(ItemStack stack) {
return stack.has(GTDataComponents.TOOL_BEHAVIORS);
return !getBehaviorsComponent(stack).isEmpty();
}

public static ItemStack get(GTToolType toolType, Material material) {
Expand Down Expand Up @@ -281,7 +282,17 @@ else if (!ItemStack.isSameItem(player.getMainHandItem(), stack)) {
}

public static AoESymmetrical getAoEDefinition(ItemStack stack) {
return stack.getOrDefault(GTDataComponents.AOE, AoESymmetrical.ZERO);
AoESymmetrical value = stack.getOrDefault(GTDataComponents.AOE, AoESymmetrical.ZERO);
if (stack.has(GTDataComponents.MAX_AOE)) {
AoESymmetrical max = stack.getOrDefault(GTDataComponents.MAX_AOE, AoESymmetrical.ZERO);
return value.min(max);
} else {
return value;
}
}

public static AoESymmetrical.Mutable getAoEStateMutable(ItemStack stack) {
return getAoEDefinition(stack).toMutable(stack.getOrDefault(GTDataComponents.MAX_AOE, AoESymmetrical.ZERO));
}

public static List<BlockPos> iterateAoE(AoESymmetrical aoeDefinition, Predicate<UseOnContext> predicate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,10 @@ private static void generateTool(final Material material, final GTToolType toolT
TOOL_ITEMS.put(material, toolType, (ItemProviderEntry<Item, ? extends IGTTool>) (ItemProviderEntry<Item, ?>) registrate
.item(toolType.idFormat.formatted(tier.material.getName()), p -> toolType.constructor.create(toolType, tier, material, toolType.toolDefinition, p).asItem())
.properties(p -> {
if (!toolType.toolDefinition.getAoEDefinition().isZero()) {
p.component(GTDataComponents.AOE, toolType.toolDefinition.getAoEDefinition());
}
return p.craftRemainder(Items.AIR);
})
.properties(p -> {
p.component(GTDataComponents.MAX_AOE, toolType.toolDefinition.getAoEDefinition());
p.component(GTDataComponents.AOE, toolType.toolDefinition.getAoEDefinition());
p.craftRemainder(Items.AIR);

IGTToolDefinition toolStats = toolType.toolDefinition;
// Set other tool stats (durability)
ToolProperty toolProperty = material.getProperty(PropertyKey.TOOL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public class GTDataComponents {
public static final DeferredHolder<DataComponentType<?>, DataComponentType<AoESymmetrical>> AOE = DATA_COMPONENTS
.registerComponentType("aoe", builder -> builder.persistent(AoESymmetrical.CODEC)
.networkSynchronized(AoESymmetrical.STREAM_CODEC));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<AoESymmetrical>> MAX_AOE = DATA_COMPONENTS
.registerComponentType("max_aoe", builder -> builder.persistent(AoESymmetrical.CODEC)
.networkSynchronized(AoESymmetrical.STREAM_CODEC));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Unit>> DISALLOW_CONTAINER_ITEM = DATA_COMPONENTS
.registerComponentType("disallow_container_item", builder -> builder.persistent(Unit.CODEC)
.networkSynchronized(UNIT_STREAM_CODEC));
Expand Down
Loading
Loading