Skip to content

Commit

Permalink
Merge remote-tracking branch 'Slimefun/master'
Browse files Browse the repository at this point in the history
DEV - 1100
  • Loading branch information
xMikux committed Sep 6, 2023
2 parents 2a11aa6 + d683a63 commit 9d70894
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/discord-webhook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3.5.3
uses: actions/checkout@v3.6.0

- name: Set up Java JDK 17
uses: actions/[email protected]
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.4.0</version>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -446,7 +446,7 @@
<dependency>
<groupId>com.gmail.nossr50.mcMMO</groupId>
<artifactId>mcMMO</artifactId>
<version>2.1.222</version>
<version>2.1.223</version>
<scope>provided</scope>

<exclusions>
Expand Down Expand Up @@ -502,7 +502,7 @@
<dependency>
<groupId>net.imprex</groupId>
<artifactId>orebfuscator-api</artifactId>
<version>5.3.3</version>
<version>5.4.0</version>
<scope>provided</scope>

<exclusions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.items.geo.GEOMiner;
import io.github.thebusybiscuit.slimefun4.implementation.items.geo.GEOScanner;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.HeadTexture;

Expand Down Expand Up @@ -236,13 +237,13 @@ public void scan(@Nonnull Player p, @Nonnull Block block, int page) {
for (int i = page * 28; i < resources.size() && i < (page + 1) * 28; i++) {
GEOResource resource = resources.get(i);
OptionalInt optional = getSupplies(resource, block.getWorld(), x, z);
int supplies = optional.isPresent() ? optional.getAsInt() : generate(resource, block.getWorld(), x, block.getY(), z);
String suffix = Slimefun.getLocalization().getResourceString(p, supplies == 1 ? "tooltips.unit" : "tooltips.units");
int supplies = optional.orElse(generate(resource, block.getWorld(), x, block.getY(), z));
String suffix = Slimefun.getLocalization().getResourceString(p, ChatUtils.checkPlurality("tooltips.unit", supplies));

ItemStack item = new CustomItemStack(resource.getItem(), "&f" + resource.getName(p), "&8\u21E8 &e" + supplies + ' ' + suffix);

if (supplies > 1) {
item.setAmount(supplies > item.getMaxStackSize() ? item.getMaxStackSize() : supplies);
item.setAmount(Math.min(supplies, item.getMaxStackSize()));
}

menu.addItem(index, item, ChestMenuUtils.getEmptyClickHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,17 @@ public final int hashCode() {
return Slimefun.getRegistry().getSlimefunItemIds().get(id);
}

/**
* Retrieve a {@link Optional}<{@link SlimefunItem}> by its id.
*
* @param id
* The id of the {@link SlimefunItem}
* @return The {@link Optional}<{@link SlimefunItem}> associated with that id. Empty if non-existent
*/
public static @Nonnull Optional<SlimefunItem> getOptionalById(@Nonnull String id) {
return Optional.ofNullable(getById(id));
}

/**
* Retrieve a {@link SlimefunItem} from an {@link ItemStack}.
*
Expand All @@ -1181,4 +1192,14 @@ public final int hashCode() {

}

/**
* Retrieve a {@link Optional}<{@link SlimefunItem}> from an {@link ItemStack}.
*
* @param item
* The {@link ItemStack} to check
* @return The {@link Optional}<{@link SlimefunItem}> associated with this {@link ItemStack} if present, otherwise empty
*/
public @Nonnull Optional<SlimefunItem> getOptionalByItem(@Nullable ItemStack item) {
return Optional.ofNullable(getByItem(item));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -493,24 +493,17 @@ public boolean hasFullProtectionAgainst(@Nonnull ProtectionType type) {

for (HashedArmorpiece armorpiece : armor) {
Optional<SlimefunArmorPiece> armorPiece = armorpiece.getItem();

if (!armorPiece.isPresent()) {
setId = null;
} else if (armorPiece.get() instanceof ProtectiveArmor protectedArmor) {
if (setId == null && protectedArmor.isFullSetRequired()) {
setId = protectedArmor.getArmorSetId();
}

for (ProtectionType protectionType : protectedArmor.getProtectionTypes()) {
if (armorPiece.isPresent() && armorPiece.get() instanceof ProtectiveArmor protectiveArmor) {
for (ProtectionType protectionType : protectiveArmor.getProtectionTypes()) {
if (protectionType == type) {
if (setId == null) {
if (!protectiveArmor.isFullSetRequired()) {
return true;
} else if (setId.equals(protectedArmor.getArmorSetId())) {
} else if (setId == null || setId.equals(protectiveArmor.getArmorSetId())) {
armorCount++;
setId = protectiveArmor.getArmorSetId();
}
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.listeners.HopperListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemDropListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.ItemPickupListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.JoinListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.MiddleClickListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.MiningAndroidListener;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.MultiBlockListener;
Expand Down Expand Up @@ -646,6 +647,7 @@ private void registerListeners() {
new BeeWingsListener(this, (BeeWings) SlimefunItems.BEE_WINGS.getItem());
new PiglinListener(this);
new SmithingTableListener(this);
new JoinListener(this);

// Item-specific Listeners
new CoolerListener(this, (Cooler) SlimefunItems.COOLER.getItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import dev.lone.itemsadder.api.CustomBlock;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Material;
Expand Down Expand Up @@ -35,9 +36,9 @@

/**
* This {@link SlimefunItem} is a super class for items like the {@link ExplosivePickaxe} or {@link ExplosiveShovel}.
*
*
* @author TheBusyBiscuit
*
*
* @see ExplosivePickaxe
* @see ExplosiveShovel
*
Expand Down Expand Up @@ -83,13 +84,21 @@ private void breakBlocks(BlockBreakEvent e, Player p, ItemStack item, Block b, L
if (!blockExplodeEvent.isCancelled()) {
for (Block block : blockExplodeEvent.blockList()) {
if (canBreak(p, block)) {
if (Slimefun.getIntegrations().isCustomBlock(block)) {
drops.addAll(CustomBlock.byAlreadyPlaced(block).getLoot());
CustomBlock.remove(block.getLocation());
}
blocksToDestroy.add(block);
}
}
}
} else {
for (Block block : blocks) {
if (canBreak(p, block)) {
if (Slimefun.getIntegrations().isCustomBlock(block)) {
drops.addAll(CustomBlock.byAlreadyPlaced(block).getLoot());
CustomBlock.remove(block.getLocation());
}
blocksToDestroy.add(block);
}
}
Expand Down Expand Up @@ -137,8 +146,6 @@ protected boolean canBreak(@Nonnull Player p, @Nonnull Block b) {
return false;
} else if (!b.getWorld().getWorldBorder().isInside(b.getLocation())) {
return false;
} else if (Slimefun.getIntegrations().isCustomBlock(b)) {
return false;
} else {
return Slimefun.getProtectionManager().hasPermission(p, b.getLocation(), Interaction.BREAK_BLOCK);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.thebusybiscuit.slimefun4.implementation.listeners;

import javax.annotation.Nonnull;

import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.api.items.HashedArmorpiece;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.armor.RadiationTask;

/**
* This {@link Listener} caches the armor of the player on join.
* This is mainly for the {@link RadiationTask}.
*
* @author iTwins
*/
public class JoinListener implements Listener {

public JoinListener(@Nonnull Slimefun plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@EventHandler
public void onJoin(@Nonnull PlayerJoinEvent e) {
PlayerProfile.get(e.getPlayer(), playerProfile -> {
final ItemStack[] armorContents = e.getPlayer().getInventory().getArmorContents();
final HashedArmorpiece[] hashedArmorpieces = playerProfile.getArmor();
for (int i = 0; i < 4; i++) {
final ItemStack armorPiece = armorContents[i];
if (armorPiece != null && armorPiece.getType() != Material.AIR && SlimefunItem.getByItem(armorPiece) instanceof SlimefunArmorPiece sfArmorPiece) {
hashedArmorpieces[i].update(armorPiece, sfArmorPiece);
}
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ protected void onPlayerTick(Player p, PlayerProfile profile) {
BaseComponent[] components = new ComponentBuilder().append(ChatColors.color(msg)).create();
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, components);
}
} else {
RadiationUtils.removeExposure(p, 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import dev.lone.itemsadder.api.CustomBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
Expand Down Expand Up @@ -236,7 +237,7 @@ public boolean isEventFaked(@Nonnull Event event) {
public boolean isCustomBlock(@Nonnull Block block) {
if (isItemsAdderInstalled) {
try {
return ItemsAdder.isCustomBlock(block);
return CustomBlock.byAlreadyPlaced(block) != null;
} catch (Exception | LinkageError x) {
logError("ItemsAdder", x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,26 @@ public static void awaitInput(@Nonnull Player p, @Nonnull Consumer<String> callb
return builder.toString();
}

/**
* This method adds an s to a string if the supplied integer is not 1.
*
* @param string
* The string to potentially pluralize
* @param count
* The amount of things
* @return
* {@code string} if {@code count} is 1 else {@code string + "s"}
* @throws IllegalArgumentException
* if count is less than 0
*/
public static @Nonnull String checkPlurality(@Nonnull String string, int count) {
if (count < 0) {
throw new IllegalArgumentException("Argument count cannot be negative.");
}
if (count == 1) {
return string;
}
return string + "s";
}

}
22 changes: 11 additions & 11 deletions src/main/java/me/mrCookieSlime/Slimefun/api/BlockStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ public BlockStorage(World w) {
this.world = w;

if (world.getName().indexOf('.') != -1) {
throw new IllegalArgumentException("Slimefun 無法處理世界名稱包含點: " + w.getName());
throw new IllegalArgumentException("Slimefun 無法處理世界名稱包含點" + w.getName());
}

if (Slimefun.getRegistry().getWorlds().containsKey(w.getName())) {
// Cancel the loading process if the world was already loaded
return;
}

Slimefun.logger().log(Level.INFO, "載入方塊給世界 \"{0}\"", w.getName());
Slimefun.logger().log(Level.INFO, "載入方塊給世界{0}", w.getName());
Slimefun.logger().log(Level.INFO, "這可能需要一些時間...");

File dir = new File(PATH_BLOCKS + w.getName());
Expand Down Expand Up @@ -150,13 +150,13 @@ private void loadBlocks(File directory) {
try {
for (File file : directory.listFiles()) {
if (file.getName().equals("null.sfb")) {
Slimefun.logger().log(Level.WARNING, "檢測到方塊損毀檔案!");
Slimefun.logger().log(Level.WARNING, "Slimefun 會跳過此檔案, 你應該檢查看看!");
Slimefun.logger().log(Level.WARNING, "偵測到方塊損毀檔案!");
Slimefun.logger().log(Level.WARNING, "Slimefun 會跳過此檔案你應該檢查看看");
Slimefun.logger().log(Level.WARNING, file.getPath());
} else if (file.getName().endsWith(".sfb")) {
if (timestamp + delay < System.currentTimeMillis()) {
int progress = Math.round((((done * 100.0F) / total) * 100.0F) / 100.0F);
Slimefun.logger().log(Level.INFO, "載入方塊... {0}% 完成 (\"{1}\")", new Object[] { progress, world.getName() });
Slimefun.logger().log(Level.INFO, "載入方塊... {0}% 完成{1}", new Object[] { progress, world.getName() });
timestamp = System.currentTimeMillis();
}

Expand All @@ -172,11 +172,11 @@ private void loadBlocks(File directory) {
}
} finally {
long time = (System.currentTimeMillis() - start);
Slimefun.logger().log(Level.INFO, "載入方塊... 100% (已完成 - {0}毫秒)", time);
Slimefun.logger().log(Level.INFO, "總共載入{0}個方塊於世界 \"{1}\"", new Object[] { totalBlocks, world.getName() });
Slimefun.logger().log(Level.INFO, "載入方塊... 100%已完成 - {0} 毫秒)", time);
Slimefun.logger().log(Level.INFO, "總共載入 {0} 個方塊於世界「{1}", new Object[] { totalBlocks, world.getName() });

if (totalBlocks > 0) {
Slimefun.logger().log(Level.INFO, "平均: {0}毫秒/方塊", NumberUtils.roundDecimalNumber((double) time / (double) totalBlocks));
Slimefun.logger().log(Level.INFO, "平均{0} 毫秒/方塊", NumberUtils.roundDecimalNumber((double) time / (double) totalBlocks));
}
}
}
Expand Down Expand Up @@ -214,7 +214,7 @@ private void loadBlock(File file, FileConfiguration cfg, String key) {
}
}
} catch (Exception x) {
Slimefun.logger().log(Level.WARNING, x, () -> "載入失敗 " + file.getName() + '(' + key + ") 於Slimefun " + Slimefun.getVersion());
Slimefun.logger().log(Level.WARNING, x, () -> "載入失敗 " + file.getName() + '(' + key + ") 於 Slimefun " + Slimefun.getVersion());
}
}

Expand All @@ -231,7 +231,7 @@ private void loadChunks() {
Slimefun.getRegistry().getChunks().put(key, data);
}
} catch (Exception x) {
Slimefun.logger().log(Level.WARNING, x, () -> "載入失敗 " + chunks.getName() + " 在世界 " + world.getName() + '(' + key + ") 於Slimefun " + Slimefun.getVersion());
Slimefun.logger().log(Level.WARNING, x, () -> "載入失敗 " + chunks.getName() + " 在世界 " + world.getName() + '(' + key + ") 於 Slimefun " + Slimefun.getVersion());
}
}
}
Expand Down Expand Up @@ -311,7 +311,7 @@ public void save() {
return;
}

Slimefun.logger().log(Level.INFO, "保存世界方塊資料 \"{0}\" ({1}個變化已排程)", new Object[] { world.getName(), changes });
Slimefun.logger().log(Level.INFO, "儲存世界方塊資料「{0}」({1} 個變化已排程)", new Object[] { world.getName(), changes });
Map<String, Config> cache = new HashMap<>(blocksCache);

for (Map.Entry<String, Config> entry : cache.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ void testColorCodeRemoval() {
Assertions.assertEquals(expected, ChatUtils.removeColorCodes(ChatColor.GREEN + "Hello " + ChatColor.RED + "world"));
}

@Test
@DisplayName("Test ChatUtils.checkPlurality(...)")
void testPluralization() {
String input = "Banana";
Assertions.assertThrows(IllegalArgumentException.class, () -> ChatUtils.checkPlurality(input, -1));
Assertions.assertEquals("Bananas", ChatUtils.checkPlurality(input, 0));
Assertions.assertEquals("Banana", ChatUtils.checkPlurality(input, 1));
Assertions.assertEquals("Bananas", ChatUtils.checkPlurality(input, 2));
}

}

0 comments on commit 9d70894

Please sign in to comment.