Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
#	src/main/java/io/github/schntgaispock/slimehud/util/HudBuilder.java
#	src/main/java/io/github/schntgaispock/slimehud/waila/HudController.java
  • Loading branch information
ybw0014 committed Sep 8, 2024
2 parents a06bc8b + 898b6cf commit 9f192b6
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 46 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ https://user-images.githubusercontent.com/101147426/180625211-cbcfd01d-d418-4e6c

## PlaceholderAPI

- `%slimehud_toggle%`
返回玩家是否已启用 SlimeHUD。`true``false`
- `%slimehud_toggle%` 返回玩家是否已启用 SlimeHUD。`true``false`
- `%slimehud_hud%` 返回完整的的 HUD 显示文本,包含方块名称与额外信息
- `%slimehud_hud_block%` 仅包含方块名称
- `%slimehud_hud_block_info%` 仅包含额外信息

## 限制

- Minecraft 仅有7种颜色的BossBar,而聊天文字颜色却有16种。
- Minecraft 仅有7种颜色的 BossBar,而聊天文字颜色却有16种。

## 需要

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.schntgaispock.slimehud</groupId>
<artifactId>SlimeHUD</artifactId>
<version>1.2.7</version>
<version>1.3.0</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.github.schntgaispock.slimehud.placeholder;

import io.github.schntgaispock.slimehud.SlimeHUD;
import io.github.schntgaispock.slimehud.waila.PlayerWAILA;
import io.github.schntgaispock.slimehud.waila.WAILAManager;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class PlaceholderHook extends PlaceholderExpansion {
private final WAILAManager wailaManager = WAILAManager.getInstance();

@Override
public @Nonnull String getIdentifier() {
return "slimehud";
Expand All @@ -32,8 +36,22 @@ public boolean persist() {
public @Nullable String onPlaceholderRequest(Player player, @Nonnull String params) {
if (params.equalsIgnoreCase("toggle")) {
return SlimeHUD.getInstance().getPlayerData().getString(player.getUniqueId() + ".waila", "true");
} else {
return null;
} else if (params.startsWith("hud")) {
String[] split = params.split("_");
PlayerWAILA playerWAILA = wailaManager.getWailas().get(player.getUniqueId());
switch (split.length) {
case 1 -> {
return playerWAILA.getFacing();
}
case 2 -> {
return playerWAILA.getFacingBlock();
}
case 3 -> {
return playerWAILA.getFacingBlockInfo();
}
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class HudBuilder {
*/
@Nonnull
public static String formatEnergyStored(int energy) {
return "&7| " + HudBuilder.getAbbreviatedNumber(energy) + " J 已存储";
return HudBuilder.getAbbreviatedNumber(energy) + " J 已存储";
}

/**
Expand All @@ -33,7 +33,7 @@ public static String formatEnergyStored(int energy) {
*/
@Nonnull
public static String formatEnergyStored(int energy, int totalEnergy) {
return "&7| " + HudBuilder.getAbbreviatedNumber(energy) + "/" + HudBuilder.getAbbreviatedNumber(totalEnergy) + " J";
return HudBuilder.getAbbreviatedNumber(energy) + "/" + HudBuilder.getAbbreviatedNumber(totalEnergy) + " J";
}

/**
Expand All @@ -44,7 +44,7 @@ public static String formatEnergyStored(int energy, int totalEnergy) {
*/
@Nonnull
public static String formatEnergyGenerated(int energy) {
return "&7| &e⚡&7 " + HudBuilder.getAbbreviatedNumber(energy) + " J/t";
return "&e⚡&7 " + HudBuilder.getAbbreviatedNumber(energy) + " J/t";
}

/**
Expand Down Expand Up @@ -128,7 +128,7 @@ public static String formatProgressBar(int progress, int total) {
*/
@Nonnull
public static String formatProgressBar(int percentCompleted) {
return "&7| " + HudBuilder.getProgressBar(percentCompleted) + " - " + percentCompleted + "%";
return HudBuilder.getProgressBar(percentCompleted) + " - " + percentCompleted + "%";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private String processEnergyNode(@Nonnull HudRequest request) {

Network en = EnergyNet.getNetworkFromLocation(request.getLocation());
int size = getNetworkSize(en);
return size < 0 ? "" : "&7| 能源网络大小: " + HudBuilder.getCommaNumber(size);
return size < 0 ? "" : "能源网络大小: " + HudBuilder.getCommaNumber(size);
}

@Nonnull
Expand Down Expand Up @@ -99,7 +99,7 @@ private String processMachine(@Nonnull HudRequest request) {
MachineOperation operation = machine.getMachineProcessor().getOperation(request.getLocation());

if (operation == null) {
hudText.append("&7| 空闲");
hudText.append("空闲");
if (request.getSlimefunItem() instanceof EnergyNetComponent) {
hudText.append(" ").append(processCapacitor(request));
}
Expand Down Expand Up @@ -131,7 +131,7 @@ private String processGenerator(@Nonnull HudRequest request) {
if (generation > 0) {
hudText.append(HudBuilder.formatEnergyGenerated(generation));
} else {
hudText.append("&7| 不在发电");
hudText.append("不在发电");
}

if (gen instanceof EnergyNetComponent) {
Expand All @@ -155,7 +155,7 @@ private String processSolarGenerator(@Nonnull HudRequest request) {
if (generation > 0) {
hudText.append(HudBuilder.formatEnergyGenerated(generation));
} else {
hudText.append("&7| 不在发电");
hudText.append("不在发电");
}

if (gen instanceof EnergyNetComponent) {
Expand All @@ -172,7 +172,7 @@ private String processCargoNode(@Nonnull HudRequest request) {
}
CargoNode cn = (CargoNode) request.getSlimefunItem();
int channel = cn.getSelectedChannel(request.getLocation().getBlock()) + 1;
return "&7| 信道: " + Util.getColorFromCargoChannel(channel).toString() + channel;
return "信道: " + Util.getColorFromCargoChannel(channel).toString() + channel;
}

@Nonnull
Expand All @@ -183,7 +183,7 @@ private String processCargoManagerConnector(@Nonnull HudRequest request) {
Network cn = CargoNet.getNetworkFromLocation(request.getLocation());

int size = getNetworkSize(cn);
return size < 0 ? "" : "&7| 货运网络大小: " + HudBuilder.getCommaNumber(size);
return size < 0 ? "" : "货运网络大小: " + HudBuilder.getCommaNumber(size);
}

private int getNetworkSize(Network network) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@

public class PlayerWAILA extends BukkitRunnable {

final private @Nonnull @Getter Player player;
final private @Nonnull
@Getter Player player;
final private @Getter BossBar WAILABar; // Bossbar
final private String WAILALocation;
final private boolean useAutoBossBarColor;
final private boolean keepTextColors;

/**
* Returns an empty string if not a Slimefun item. Otherwise returns the
* formatted item name
*/
@Getter
private String facing = "";
@Getter
private String facingBlock = "";
@Getter
private String facingBlockInfo = "";
private String previousFacing = "";

private @Getter boolean paused;

public PlayerWAILA(@Nonnull Player player) {
Expand All @@ -44,39 +55,13 @@ public PlayerWAILA(@Nonnull Player player) {
this.keepTextColors = SlimeHUD.getInstance().getConfig().getBoolean("waila.use-original-colors");
}

/**
* Returns an empty string if not a Slimefun item. Otherwise returns the
* formatted item name
*
* @return Formatted item name or empty string
*/
public String getFacing() {
Block targetBlock = player.getTargetBlockExact(5);
if (targetBlock == null)
return "";

Location target = targetBlock.getLocation();
if (target == null)
return "";

SlimefunItem item = BlockStorage.check(targetBlock);
if (item == null)
return "";

HudRequest request = new HudRequest(item, target, player);
StringBuilder text = new StringBuilder(SlimeHUD.getTranslationManager().getItemName(player, item))
.append(" ")
.append(ChatColor.translateAlternateColorCodes('&',
SlimeHUD.getHudController().processRequest(request)));

return text.toString();
}

/**
* Called every <code>waila.tick-rate</code> ticks
*/
@Override
public void run() {
updateFacing();

if (isPaused()) {
return;
}
Expand Down Expand Up @@ -113,6 +98,32 @@ public void run() {

}

private void updateFacing() {
Block targetBlock = player.getTargetBlockExact(5);
if (targetBlock == null) {
clearFacing();
return;
}

SlimefunItem item = BlockStorage.check(targetBlock);
if (item == null) {
clearFacing();
return;
}

Location target = targetBlock.getLocation();
HudRequest request = new HudRequest(item, target, player);
facingBlock = SlimeHUD.getTranslationManager().getItemName(player, item);
facingBlockInfo = SlimeHUD.getHudController().processRequest(request);
facing = ChatColor.translateAlternateColorCodes('&', facingBlock + (facingBlockInfo.isEmpty() ? "" : " &7| " + facingBlockInfo));
}

private void clearFacing() {
facingBlock = "";
facingBlockInfo = "";
facing = "";
}

public void setPaused(boolean paused) {
setVisible(!previousFacing.equals("") && !paused);
this.paused = paused;
Expand Down

0 comments on commit 9f192b6

Please sign in to comment.