Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
对于堆叠的可充电物品采取了逐一充电的方法
  • Loading branch information
mcchampions committed Jul 5, 2024
1 parent ac3a76f commit 7328627
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
* The {@link ChargingBench} is a powered machine that can be used to charge any {@link Rechargeable} item.
*
* @author TheBusyBiscuit
*
* @see Rechargeable
*
*/
public class ChargingBench extends AContainer {

Expand All @@ -41,13 +39,40 @@ protected void tick(Block b) {

for (int slot : getInputSlots()) {
ItemStack item = inv.getItemInSlot(slot);

if (item.getAmount() != 1) {
if (chargeOne(b, inv, slot, item)) {
return;
}
}
if (charge(b, inv, slot, item)) {
return;
}
}
}


private boolean chargeOne(Block b, BlockMenu inv, int slot, ItemStack item) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);

if (sfItem instanceof Rechargeable rechargeable) {
float charge = getEnergyConsumption() / 2F;

if (rechargeable.addItemCharge(item, charge)) {
removeCharge(b.getLocation(), getEnergyConsumption());
} else if (inv.fits(item, getOutputSlots())) {
inv.pushItem((SlimefunItemStack) sfItem.getItem(), getOutputSlots());
item.setAmount(item.getAmount() - 1);
}

return true;
} else if (sfItem != null && inv.fits(item, getOutputSlots())) {
inv.pushItem((SlimefunItemStack) sfItem.getItem(), getOutputSlots());
// 不可充电物品直接过
inv.replaceExistingItem(slot, null);
}
return false;
}

private boolean charge(Block b, BlockMenu inv, int slot, ItemStack item) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);

Expand All @@ -57,16 +82,15 @@ private boolean charge(Block b, BlockMenu inv, int slot, ItemStack item) {
if (rechargeable.addItemCharge(item, charge)) {
removeCharge(b.getLocation(), getEnergyConsumption());
} else if (inv.fits(item, getOutputSlots())) {
inv.pushItem(item, getOutputSlots());
inv.pushItem((SlimefunItemStack) sfItem.getItem(), getOutputSlots());
inv.replaceExistingItem(slot, null);
}

return true;
} else if (sfItem != null && inv.fits(item, getOutputSlots())) {
inv.pushItem(item, getOutputSlots());
inv.pushItem((SlimefunItemStack) sfItem.getItem(), getOutputSlots());
inv.replaceExistingItem(slot, null);
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ public static ItemStack getCustomHead(String texture) {

if (CommonPatterns.HEXADECIMAL.matcher(texture).matches()) {
base64 = Base64.getEncoder()
.encodeToString(("{\"textures\":{\"SKIN\":{\"url\":\"http://textures.minecraft.net/texture/"
+ texture
+ "\"}}}")
.getBytes(StandardCharsets.UTF_8));
.encodeToString(("{\"textures\":{\"SKIN\":{\"url\":\"http://textures.minecraft.net/texture/"
+ texture
+ "\"}}}")
.getBytes(StandardCharsets.UTF_8));
}

PlayerSkin skin = PlayerSkin.fromBase64(base64);
Expand Down Expand Up @@ -261,26 +261,41 @@ public static boolean isItemSimilar(@Nullable ItemStack item, @Nullable ItemStac
}

public static boolean isItemSimilar(
@Nullable ItemStack item, @Nullable ItemStack sfitem, boolean checkLore, boolean checkAmount) {
@Nullable ItemStack item, @Nullable ItemStack sfitem, boolean checkLore, boolean checkAmount) {
return isItemSimilar(item, sfitem, checkLore, checkAmount, true, true);
}

public static boolean isItemSimilar(
@Nullable ItemStack item,
@Nullable ItemStack sfitem,
boolean checkLore,
boolean checkAmount,
boolean checkDistinctiveItem) {
@Nullable ItemStack item,
@Nullable ItemStack sfitem,
boolean checkLore,
boolean checkAmount,
boolean checkDistinctiveItem) {
return isItemSimilar(item, sfitem, checkLore, checkAmount, checkDistinctiveItem, true);
}

public static boolean isSlimefunItemSimilar(SlimefunItemStack sfItem, ItemStack item) {
ItemStack tempStack = SlimefunItem.getByItem(item).getItem();
if (tempStack == null) {
return false;
}
SlimefunItemStack sfItemStack = (SlimefunItemStack) tempStack;
if (sfItem.getItemId().equals(sfItemStack.getItemId())) {
if (sfItem instanceof DistinctiveItem && sfItemStack instanceof DistinctiveItem distinctiveItem) {
return distinctiveItem.canStack(sfItem.getItemMeta(), item.getItemMeta());
}
return true;
}
return false;
}

public static boolean isItemSimilar(
@Nullable ItemStack item,
@Nullable ItemStack sfitem,
boolean checkLore,
boolean checkAmount,
boolean checkDistinctiveItem,
boolean checkCustomModelData) {
@Nullable ItemStack item,
@Nullable ItemStack sfitem,
boolean checkLore,
boolean checkAmount,
boolean checkDistinctiveItem,
boolean checkCustomModelData) {
if (item == null) {
return sfitem == null;
} else if (sfitem == null
Expand All @@ -290,19 +305,7 @@ public static boolean isItemSimilar(
} else if (checkDistinctiveItem
&& sfitem instanceof SlimefunItemStack stackOne
&& item instanceof SlimefunItemStack stackTwo) {
if (stackOne.getItemId().equals(stackTwo.getItemId())) {
/*
* PR #3417
*
* Some items can't rely on just IDs matching and will implement Distinctive Item
* in which case we want to use the method provided to compare
*/
if (stackOne instanceof DistinctiveItem && stackTwo instanceof DistinctiveItem distinctiveItem) {
return distinctiveItem.canStack(stackOne.getItemMeta(), stackTwo.getItemMeta());
}
return true;
}
return false;
return isSlimefunItemSimilar(stackOne, stackTwo);
} else if (item.hasItemMeta()) {
Debug.log(TestCase.CARGO_INPUT_TESTING, "SlimefunUtils#isItemSimilar - item.hasItemMeta()");
ItemMeta itemMeta = item.getItemMeta();
Expand Down Expand Up @@ -341,7 +344,7 @@ public static boolean isItemSimilar(
ItemMeta sfItemMeta = sfitem.getItemMeta();
String possibleItemId = Slimefun.getItemDataService().getItemData(itemMeta).orElse(null);
String sfItemId = Slimefun.getItemDataService()
.getItemData(sfItemMeta).get();
.getItemData(sfItemMeta).get();
// Prioritize SlimefunItem id comparison over ItemMeta comparison
if (possibleItemId != null && possibleItemId.equals(sfItemId)) {
Debug.log(TestCase.CARGO_INPUT_TESTING, " SlimefunItem IDs matched!");
Expand All @@ -353,20 +356,17 @@ public static boolean isItemSimilar(
* in which case we want to use the method provided to compare
*/
Optional<DistinctiveItem> optionalDistinctive = getDistinctiveItem(possibleItemId);
if (optionalDistinctive.isPresent()) {
return optionalDistinctive.get().canStack(sfItemMeta, itemMeta);
}
return true;
return optionalDistinctive.map(distinctiveItem -> distinctiveItem.canStack(sfItemMeta, itemMeta)).orElse(true);
}
return false;
} else if (sfitem.hasItemMeta()) {
ItemMeta sfItemMeta = sfitem.getItemMeta();
Debug.log(
TestCase.CARGO_INPUT_TESTING,
" Comparing meta (vanilla items?) - {} == {} (lore: {})",
itemMeta,
sfItemMeta,
checkLore);
TestCase.CARGO_INPUT_TESTING,
" Comparing meta (vanilla items?) - {} == {} (lore: {})",
itemMeta,
sfItemMeta,
checkLore);
return equalsItemMeta(itemMeta, sfItemMeta, checkLore, !checkCustomModelData);
} else {
return false;
Expand All @@ -385,15 +385,15 @@ private static Optional<DistinctiveItem> getDistinctiveItem(String id) {
}

private static boolean equalsItemMeta(
ItemMeta itemMeta, ItemMetaSnapshot itemMetaSnapshot, boolean checkLore) {
ItemMeta itemMeta, ItemMetaSnapshot itemMetaSnapshot, boolean checkLore) {
return equalsItemMeta(itemMeta, itemMetaSnapshot, checkLore, true);
}

private static boolean equalsItemMeta(
ItemMeta itemMeta,
ItemMetaSnapshot itemMetaSnapshot,
boolean checkLore,
boolean bypassCustomModelCheck) {
ItemMeta itemMeta,
ItemMetaSnapshot itemMetaSnapshot,
boolean checkLore,
boolean bypassCustomModelCheck) {
Optional<String> displayName = itemMetaSnapshot.getDisplayName();

if (itemMeta.hasDisplayName() != displayName.isPresent()) {
Expand Down Expand Up @@ -428,10 +428,10 @@ private static boolean equalsItemMeta(
}

private static boolean equalsItemMeta(
ItemMeta itemMeta,
ItemMeta sfitemMeta,
boolean checkLore,
boolean bypassCustomModelCheck) {
ItemMeta itemMeta,
ItemMeta sfitemMeta,
boolean checkLore,
boolean bypassCustomModelCheck) {
if (itemMeta.hasDisplayName() != sfitemMeta.hasDisplayName()) {
return false;
} else if (itemMeta.hasDisplayName()
Expand Down Expand Up @@ -563,7 +563,7 @@ public static boolean canPlayerUseItem(Player p, @Nullable ItemStack item, boole
*/
@ParametersAreNonnullByDefault
public static @Nullable Item spawnItem(
Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset, @Nullable Player player) {
Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset, @Nullable Player player) {
SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(player, loc, item, reason);
Slimefun.instance().getServer().getPluginManager().callEvent(event);

Expand Down Expand Up @@ -593,7 +593,7 @@ public static boolean canPlayerUseItem(Player p, @Nullable ItemStack item, boole
*/
@ParametersAreNonnullByDefault
public static @Nullable Item spawnItem(
Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) {
Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) {
return spawnItem(loc, item, reason, addRandomOffset, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.bakedlibs.dough.items.CustomItemStack;
import io.github.bakedlibs.dough.items.ItemUtils;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.core.debug.Debug;
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
Expand Down Expand Up @@ -199,6 +200,51 @@ public boolean fits(ItemStack item, int... slots) {
}
}

/**
* Adds given {@link ItemStack} to any of the given inventory slots.
* Items will be added to the inventory slots based on their order in the function argument.
* Items will be added either to any empty inventory slots or any partially filled slots, in which case
* as many items as can fit will be added to that specific spot.
*
* @param item {@link ItemStack} to be added to the inventory
* @param slots Numbers of slots to add the {@link ItemStack} to
* @return {@link ItemStack} with any items that did not fit into the inventory
* or null when everything had fit
*/
@Nullable public ItemStack pushItem(SlimefunItemStack item, int... slots) {
int amount = item.getAmount();

for (int slot : slots) {
if (amount <= 0) {
break;
}

ItemStack stack = getItemInSlot(slot);
if (stack == null) {
replaceExistingItem(slot, item);
return null;
} else {
int maxStackSize =
Math.min(stack.getMaxStackSize(), toInventory().getMaxStackSize());
if (stack.getAmount() < maxStackSize) {
if (!SlimefunUtils.isSlimefunItemSimilar(item,stack)) {
continue;
}

amount -= (maxStackSize - stack.getAmount());
stack.setAmount(Math.min(stack.getAmount() + item.getAmount(), maxStackSize));
item.setAmount(amount);
}
}
}

if (amount > 0) {
return new CustomItemStack(item, amount);
} else {
return null;
}
}

public void consumeItem(int slot) {
consumeItem(slot, 1);
}
Expand Down

0 comments on commit 7328627

Please sign in to comment.