Skip to content

Commit

Permalink
beta3
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiao-MoMi committed Oct 8, 2024
1 parent 6b2bbea commit a387cf5
Show file tree
Hide file tree
Showing 30 changed files with 568 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public abstract class AbstractCNPlayer implements CNPlayer {

private boolean isLoaded = false;

private boolean isPreviewing = false;
private boolean tempPreviewing = false;
private boolean toggleablePreviewing = false;

private String equippedNameplate;
private String equippedBubble;
Expand Down Expand Up @@ -179,13 +180,22 @@ public void setLoaded(boolean loaded) {
isLoaded = loaded;
}

public void setPreviewing(boolean previewing) {
isPreviewing = previewing;
public void setTempPreviewing(boolean previewing) {
this.tempPreviewing = previewing;
}

@Override
public boolean isPreviewing() {
return isPreviewing;
public boolean isTempPreviewing() {
return tempPreviewing;
}

public void setToggleablePreviewing(boolean previewing) {
this.toggleablePreviewing = previewing;
}

@Override
public boolean isToggleablePreviewing() {
return toggleablePreviewing;
}

@Override
Expand Down Expand Up @@ -468,6 +478,7 @@ public void save() {
.uuid(uuid())
.nameplate(equippedNameplate())
.bubble(equippedBubble())
.previewTags(isToggleablePreviewing())
.build(), plugin.getScheduler().async());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ public interface CNPlayer {
*
* @return true if the player is previewing, false otherwise
*/
boolean isPreviewing();
boolean isTempPreviewing();

/**
* Checks if the player has turned their nameplate on
*
* @return true if the player has turned their nameplate on, false otherwise
*/
boolean isToggleablePreviewing();

/**
* Checks if the player has the specified permission.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static YamlDocument getMainConfig() {
protected int defaultConditionRefreshInterval;
protected int delaySend;
protected boolean catchOtherActionBar;
protected int otherActionBarStayTime;

protected String namespace;
protected String font;
Expand Down Expand Up @@ -213,6 +214,7 @@ private void loadSettings() {
defaultPlaceholderRefreshInterval = config.getInt("other-settings.default-placeholder-refresh-interval", 1);
defaultConditionRefreshInterval = config.getInt("other-settings.ddefault-condition-refresh-interval", 1);
catchOtherActionBar = config.getBoolean("other-settings.catch-other-plugin-actionbar", true);
otherActionBarStayTime = config.getInt("other-settings.other-actionbar-stay-time", 3000);
}

@Override
Expand Down Expand Up @@ -336,6 +338,10 @@ public static int defaultConditionRefreshInterval() {
return instance.defaultConditionRefreshInterval;
}

public static int otherActionBarStayTime() {
return instance.otherActionBarStayTime;
}

public static boolean enableShader() {
return instance.enableShader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public abstract class CustomNameplates implements NameplatesPlugin {
protected ImageManager imageManager;
protected NameplateManager nameplateManager;
protected ResourcePackManager resourcePackManager;
protected CustomNameplatesAPI api;

protected CustomNameplates() {
instance = this;
Expand Down Expand Up @@ -262,6 +263,15 @@ public ResourcePackManager getResourcePackManager() {
return resourcePackManager;
}

/**
* Get the API class
*
* @return api
*/
public CustomNameplatesAPI getAPI() {
return api;
}

/**
* Returns the platform object for accessing platform-specific functionality.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.momirealms.customnameplates.api;

import net.momirealms.customnameplates.api.feature.AdaptiveImage;
import net.momirealms.customnameplates.api.feature.OffsetFont;
import net.momirealms.customnameplates.api.feature.background.Background;
import net.momirealms.customnameplates.api.feature.bubble.Bubble;
import net.momirealms.customnameplates.api.feature.nameplate.Nameplate;
import net.momirealms.customnameplates.api.helper.AdventureHelper;
import net.momirealms.customnameplates.api.placeholder.internal.StaticPosition;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.UUID;

public record CustomNameplatesAPI(CustomNameplates plugin) {

private static CustomNameplatesAPI instance;

public CustomNameplatesAPI(CustomNameplates plugin) {
this.plugin = plugin;
instance = this;
}

/**
* Gets the CustomNameplates plugin instance.
*
* @return the CustomNameplates plugin
*/
@Override
public CustomNameplates plugin() {
return plugin;
}

/**
* Retrieves a player by their UUID.
*
* @param uuid the player's UUID
* @return the CNPlayer object if found, otherwise null
*/
@Nullable
public CNPlayer getPlayer(UUID uuid) {
return plugin.getPlayer(uuid);
}

/**
* Retrieves a Background by its ID.
*
* @param id the background ID
* @return an Optional containing the Background if found, or empty if not
*/
@NotNull
public Optional<Background> getBackground(@NotNull String id) {
return Optional.ofNullable(plugin.getBackgroundManager().backgroundById(id));
}

/**
* Retrieves a Nameplate by its ID.
*
* @param id the nameplate ID
* @return an Optional containing the Nameplate if found, or empty if not
*/
@NotNull
public Optional<Nameplate> getNameplate(@NotNull String id) {
return Optional.ofNullable(plugin.getNameplateManager().nameplateById(id));
}

/**
* Retrieves a Bubble by its ID.
*
* @param id the bubble ID
* @return an Optional containing the Bubble if found, or empty if not
*/
@NotNull
public Optional<Bubble> getBubble(@NotNull String id) {
return Optional.ofNullable(plugin.getBubbleManager().bubbleById(id));
}

/**
* Creates a text string with an image, applying margins around the image.
*
* @param text the text to be displayed
* @param adaptiveImage the image to insert with the text
* @param leftMargin the margin to apply on the left of the image
* @param rightMargin the margin to apply on the right of the image
* @return the resulting string with the image and margins
*/
@NotNull
public String createTextWithImage(@NotNull String text, @NotNull AdaptiveImage adaptiveImage, float leftMargin, float rightMargin) {
if (AdventureHelper.legacySupport) {
text = AdventureHelper.legacyToMiniMessage(text);
}
float advance = plugin.getAdvanceManager().getLineAdvance(text);
return adaptiveImage.createImagePrefix(advance, leftMargin, rightMargin) + text + adaptiveImage.createImageSuffix(advance, leftMargin, rightMargin);
}

/**
* Gets the width advance (text length in visual representation) for a given text.
*
* @param text the text to measure
* @return the advance width of the text
*/
public float getTextAdvance(@NotNull String text) {
return plugin.getAdvanceManager().getLineAdvance(text);
}

/**
* Creates a statically positioned text, aligning it to the left, right, or middle
* of the specified width.
*
* @param text the text to be positioned
* @param width the total width for the text positioning
* @param position the desired static position (LEFT, RIGHT, MIDDLE)
* @return the resulting string with the appropriate positioning
*/
@NotNull
public String createStaticText(@NotNull String text, int width, @NotNull StaticPosition position) {
float parsedWidth = CustomNameplates.getInstance().getAdvanceManager().getLineAdvance(text);
switch (position) {
case LEFT -> {
return text + AdventureHelper.surroundWithNameplatesFont(OffsetFont.createOffsets(width - parsedWidth));
}
case RIGHT -> {
return AdventureHelper.surroundWithNameplatesFont(OffsetFont.createOffsets(width - parsedWidth)) + text;
}
case MIDDLE -> {
int half = (int) ((width - parsedWidth) / 2);
String left = AdventureHelper.surroundWithNameplatesFont(OffsetFont.createOffsets(half));
String right = AdventureHelper.surroundWithNameplatesFont(OffsetFont.createOffsets(width - parsedWidth - half));
return left + text + right;
}
default -> {
return "";
}
}
}

/**
* Returns the singleton instance of CustomNameplatesAPI.
* Throws an exception if the API has not been initialized.
*
* @return the CustomNameplatesAPI instance
*/
public static CustomNameplatesAPI getInstance() {
if (instance == null) {
throw new RuntimeException("Nameplates API has not been initialized yet.");
}
return instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.momirealms.customnameplates.api.feature;

import net.momirealms.customnameplates.api.CNPlayer;

/**
* Listener for handling player respawn
*/
public interface RespawnListener {

/**
* Called when a player respawns
*
* @param player the player who respawns
*/
void onRespawn(CNPlayer player);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.momirealms.customnameplates.api.feature;

import net.momirealms.customnameplates.api.CNPlayer;

/**
* Listener for handling player change worlds
*/
public interface WorldChangeListener {

/**
* Called when a player changes the world
*
* @param player the player who changes the world
*/
void onChangeWorld(CNPlayer player);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ public interface UnlimitedTagManager extends Reloadable {
* @param player the player to update
* @param preview true to enable preview mode, false to disable
*/
void setPreviewing(CNPlayer player, boolean preview);
void setTempPreviewing(CNPlayer player, boolean preview);

/**
* Sets whether a player can always see their tags
*
* @param player the player to update
* @param preview true to enable preview mode, false to disable
*/
void togglePreviewing(CNPlayer player, boolean preview);

/**
* Returns the duration (in ticks) for which a tag preview is shown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import net.momirealms.customnameplates.api.CNPlayer;
import net.momirealms.customnameplates.api.CustomNameplates;
import net.momirealms.customnameplates.api.CustomNameplatesAPI;
import net.momirealms.customnameplates.api.feature.OffsetFont;
import net.momirealms.customnameplates.api.feature.PreParsedDynamicText;
import net.momirealms.customnameplates.api.helper.AdventureHelper;
Expand Down Expand Up @@ -62,23 +63,6 @@ public String create(CNPlayer p1, CNPlayer p2) {
}

public String create(String text) {
float parsedWidth = CustomNameplates.getInstance().getAdvanceManager().getLineAdvance(text);
switch (position) {
case LEFT -> {
return text + AdventureHelper.surroundWithNameplatesFont(OffsetFont.createOffsets(width - parsedWidth));
}
case RIGHT -> {
return AdventureHelper.surroundWithNameplatesFont(OffsetFont.createOffsets(width - parsedWidth)) + text;
}
case MIDDLE -> {
int half = (int) ((width - parsedWidth) / 2);
String left = AdventureHelper.surroundWithNameplatesFont(OffsetFont.createOffsets(half));
String right = AdventureHelper.surroundWithNameplatesFont(OffsetFont.createOffsets(width - parsedWidth - half));
return left + text + right;
}
default -> {
return "";
}
}
return CustomNameplatesAPI.getInstance().createStaticText(text, width, position);
}
}
Loading

0 comments on commit a387cf5

Please sign in to comment.