Skip to content

Commit

Permalink
Fixed invalid tool part crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
kill05 committed Jun 11, 2024
1 parent b65c686 commit 2162c15
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
27 changes: 16 additions & 11 deletions src/main/java/com/github/kill05/ArchitectTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,25 @@ public static Item item(String name, String texture) {


// Tool Parts
public static @NotNull ItemStack createPartStack(@NotNull ArchitectMaterial material, ArchitectPart type) {
return material.createPart(type);
public static @NotNull ItemStack createPartStack(@NotNull ArchitectMaterial material, ArchitectPart part) throws ArchitectItemException {
if(!material.isValidPart(part))
throw new ArchitectItemException(String.format("Part '%s' can't be made out of material '%s'", part.getPartId(), material.id()));

ItemStack item = new ItemStack(part);
setPartMaterial(item, material);
return item;
}

public static @NotNull ItemStack createPartStack(@NotNull String material, @NotNull ArchitectPart part) throws InvalidMaterialException {
ArchitectMaterial architectMaterial = getMaterial(material);
if (architectMaterial == null) throw new InvalidMaterialException("Invalid material: " + material);
return architectMaterial.createPart(part);
public static @NotNull ItemStack createPartStack(@NotNull String materialName, @NotNull ArchitectPart part) throws InvalidMaterialException, ArchitectItemException {
ArchitectMaterial material = getMaterial(materialName);
if (material == null) throw new InvalidMaterialException("Invalid material: " + materialName);
return createPartStack(material, part);
}

public static @NotNull ItemStack createPartStack(@NotNull ItemStack material, @NotNull ArchitectPart part) throws InvalidMaterialException {
ArchitectMaterial architectMaterial = getMaterial(material);
if (architectMaterial == null) throw new InvalidMaterialException("Invalid material: " + material);
return architectMaterial.createPart(part);
public static @NotNull ItemStack createPartStack(@NotNull ItemStack materialItem, @NotNull ArchitectPart part) throws InvalidMaterialException, ArchitectItemException {
ArchitectMaterial material = getMaterial(materialItem);
if (material == null) throw new InvalidMaterialException("Invalid material: " + materialItem);
return createPartStack(material, part);
}

public static @Nullable ArchitectMaterial getPartMaterial(@NotNull ItemStack item) {
Expand Down Expand Up @@ -269,7 +274,7 @@ public void afterClientStart() {
}
Iterator<Path> it = walk.iterator();

while(it.hasNext()) {
while (it.hasNext()) {
Path file = it.next();
String name = file.getFileName().toString();
if (name.endsWith(".png")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.github.kill05.ArchitectTools;
import com.github.kill05.blocks.architectstation.ArchitectTableTileEntity;
import com.github.kill05.exceptions.ArchitectItemException;
import com.github.kill05.exceptions.InvalidMaterialException;
import com.github.kill05.inventory.OutputInventory;
import com.github.kill05.items.part.ArchitectPart;
import com.github.kill05.materials.ArchitectMaterial;
import com.github.kill05.utils.ItemUtils;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.player.inventory.IInventory;
Expand Down Expand Up @@ -52,15 +52,15 @@ private OutputSlotInventory(ArchitectTableTileEntity tile) {

@Override
public @Nullable ItemStack getOutput() {
ArchitectPart part = tile.getSelectedPart();
ItemStack materialItem = tile.getPartInventory().getStackInSlot(1);
ArchitectMaterial material = materialItem != null ? ArchitectTools.getMaterial(materialItem) : null;
if(tile.getSelectedPart() == null) return null;
if(!ItemUtils.compare(tile.getPartInventory().getStackInSlot(0), ArchitectTools.BLANK_PATTERN)) return null;

boolean hasOutput =
ItemUtils.compare(tile.getPartInventory().getStackInSlot(0), ArchitectTools.BLANK_PATTERN) &&
part != null && material != null;

return hasOutput ? material.createPart(part) : null;
try {
ItemStack materialItem = tile.getPartInventory().getStackInSlot(1);
return ArchitectTools.createPartStack(materialItem, tile.getSelectedPart());
} catch (InvalidMaterialException | ArchitectItemException e) {
return null;
}
}
}
}
19 changes: 10 additions & 9 deletions src/main/java/com/github/kill05/materials/ArchitectMaterial.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.github.kill05.MiningLevel;
import com.github.kill05.items.part.ArchitectPart;
import com.github.kill05.items.part.PartType;
import com.github.kill05.items.part.statistics.PartStatistics;
import com.github.kill05.items.part.statistics.PartStatistic;
import com.github.kill05.items.part.statistics.PartStatistics;
import net.minecraft.core.block.Block;
import net.minecraft.core.data.registry.Registries;
import net.minecraft.core.data.registry.Registry;
Expand All @@ -16,8 +16,8 @@
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.*;

public class ArchitectMaterial {

Expand Down Expand Up @@ -102,13 +102,6 @@ public ArchitectMaterial(String id, String color) {
}


public @NotNull ItemStack createPart(ArchitectPart part) {
ItemStack item = new ItemStack(part);
ArchitectTools.setPartMaterial(item, this);
return item;
}


public ArchitectMaterial addItems(IItemConvertible... items) {
for (IItemConvertible item : items) {
this.items.add(item.asItem().getDefaultStack());
Expand Down Expand Up @@ -176,6 +169,14 @@ public PartStatistics getExtraStatistics() {
return getStatistics(PartType.EXTRA);
}

public boolean isValidPart(ArchitectPart part) {
for (PartType type : part.getValidTypes()) {
if(getStatistics(type) != null) return true;
}

return false;
}


public String getTranslatedName() {
return I18n.getInstance().translateNameKey("material." + ArchitectTools.MOD_ID + "." + id());
Expand Down

0 comments on commit 2162c15

Please sign in to comment.