-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Total overhaul of the remote widget system
Remote widgets are now immutable objects in a registry with no client code All client code moved to RemoteClientRegistry Remote widgets are fully codec'd and saved in data component on the remote Exported Json format much cleaner now; support for legacy import
- Loading branch information
Showing
68 changed files
with
1,864 additions
and
1,881 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/api/java/me/desht/pneumaticcraft/api/misc/IGlobalVariableHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package me.desht.pneumaticcraft.api.misc; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
public interface IGlobalVariableHelper { | ||
/** | ||
* Retrieve a blockpos variable from the GVM. The variable may start with "#" or "%" to indicate player-global | ||
* or global respectively. Missing prefix defaults to player-global. | ||
* @param id the ID of the owning player (ignored for "%" global variables) | ||
* @param varName the variable name, optionally prefixed with "%" or "#" | ||
* @param def default value if not present in the GVM | ||
* @return the variable's value | ||
*/ | ||
BlockPos getPos(@Nullable UUID id, String varName, BlockPos def); | ||
|
||
/** | ||
* Retrieve a blockpos variable from the GVM. The variable may start with "#" or "%" to indicate player-global | ||
* or global respectively. Missing prefix defaults to player-global. | ||
* @param id the ID of the owning player (ignored for "%" global variables, must be a valid player UUID otherwise) | ||
* @param varName the variable name, optionally prefixed with "%" or "#" | ||
* @return the variable's value | ||
*/ | ||
BlockPos getPos(@Nullable UUID id, String varName); | ||
|
||
/** | ||
* Retrieve an itemstack variable from the GVM. The variable may start with "#" or "%" to indicate player-global | ||
* or global respectively. Missing prefix defaults to player-global "#". | ||
* @param id the ID of the owning player (ignored for "%" global variables, must be a valid player UUID otherwise) | ||
* @param varName the variable name, optionally prefixed with "%" or "#" | ||
* @param def default value if not present in the GVM | ||
* @return the variable's value | ||
*/ | ||
ItemStack getStack(@Nullable UUID id, String varName, ItemStack def); | ||
|
||
/** | ||
* Retrieve an itemstack variable from the GVM. The variable may start with "#" or "%" to indicate player-global | ||
* or global respectively. Missing prefix defaults to player-global. | ||
* @param id the ID of the owning player (ignored for "%" global variables, must be a valid player UUID otherwise) | ||
* @param varName the variable name, optionally prefixed with "%" or "#" | ||
* @return the variable's value | ||
*/ | ||
ItemStack getStack(@Nullable UUID id, String varName); | ||
|
||
int getInt(UUID id, String varName); | ||
|
||
void setPos(UUID id, String varName, BlockPos pos); | ||
|
||
void setStack(UUID id, String varName, ItemStack stack); | ||
|
||
boolean getBool(UUID id, String varName); | ||
|
||
/** | ||
* Given a plain variable name, add the "#" or "%" prefix as appropriate | ||
* @param varName the variable | ||
* @param playerGlobal true if a player-global, false if a server-global | ||
* @return the prefixed var name | ||
*/ | ||
String getPrefixedVar(String varName, boolean playerGlobal); | ||
|
||
/** | ||
* Get the correct var prefix | ||
* @param playerGlobal true if a player-global, false if a server-global | ||
* @return the var prefix | ||
*/ | ||
String getVarPrefix(boolean playerGlobal); | ||
|
||
/** | ||
* Strip the prefix character from a var name | ||
* @param varName the var name | ||
* @return the var name without a prefix | ||
*/ | ||
String stripVarPrefix(String varName); | ||
|
||
/** | ||
* Check if this varname has a prefix character | ||
* @param varName the var name | ||
* @return true if prefixed, false otherwise | ||
*/ | ||
boolean hasPrefix(String varName); | ||
|
||
/** | ||
* Parse a string, and extract a set of global variables (both player-global and server-global) referred to in the | ||
* string via {@code ${varname}} notation. | ||
* | ||
* @param string the string to parse | ||
* @param playerId UUID of the player | ||
* @return a set of global variable names | ||
*/ | ||
Set<String> getRelevantVariables(String string, UUID playerId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/api/java/me/desht/pneumaticcraft/api/remote/BaseSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package me.desht.pneumaticcraft.api.remote; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.network.codec.ByteBufCodecs; | ||
import net.minecraft.network.codec.StreamCodec; | ||
|
||
public record BaseSettings(String enableVariable, BlockPos enablingValue) { | ||
public static final Codec<BaseSettings> CODEC = RecordCodecBuilder.create(builder -> builder.group( | ||
Codec.STRING.optionalFieldOf("enable_var", "").forGetter(BaseSettings::enableVariable), | ||
BlockPos.CODEC.optionalFieldOf("enable_pos", BlockPos.ZERO).forGetter(BaseSettings::enablingValue) | ||
).apply(builder, BaseSettings::new)); | ||
public static final StreamCodec<FriendlyByteBuf, BaseSettings> STREAM_CODEC = StreamCodec.composite( | ||
ByteBufCodecs.STRING_UTF8, BaseSettings::enableVariable, | ||
BlockPos.STREAM_CODEC, BaseSettings::enablingValue, | ||
BaseSettings::new | ||
); | ||
|
||
public static final BaseSettings DEFAULT = new BaseSettings("", BlockPos.ZERO); | ||
|
||
public BaseSettings withVariable(String var) { | ||
return new BaseSettings(var, enablingValue); | ||
} | ||
|
||
public BaseSettings withEnablingValue(BlockPos value) { | ||
return new BaseSettings(enableVariable, value); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/api/java/me/desht/pneumaticcraft/api/remote/IRemoteVariableWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package me.desht.pneumaticcraft.api.remote; | ||
|
||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
public interface IRemoteVariableWidget extends IRemoteWidget { | ||
String varName(); | ||
|
||
@Override | ||
default void discoverVariables(Set<String> variables, UUID playerId) { | ||
IRemoteWidget.super.discoverVariables(variables, playerId); | ||
|
||
if (!varName().isEmpty()) { | ||
variables.add(varName()); | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/api/java/me/desht/pneumaticcraft/api/remote/IRemoteWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package me.desht.pneumaticcraft.api.remote; | ||
|
||
import com.mojang.serialization.Codec; | ||
import me.desht.pneumaticcraft.api.PneumaticRegistry; | ||
import me.desht.pneumaticcraft.api.misc.IGlobalVariableHelper; | ||
import me.desht.pneumaticcraft.api.registry.PNCRegistries; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.network.RegistryFriendlyByteBuf; | ||
import net.minecraft.network.codec.ByteBufCodecs; | ||
import net.minecraft.network.codec.StreamCodec; | ||
import net.minecraft.world.entity.player.Player; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Represents a template for a widget to be added to the Remote GUI. This is an immutable object, is used in itemstack | ||
* data components, and does not contain any client-only methods; see {@link xxx} for that. | ||
*/ | ||
public interface IRemoteWidget { | ||
int TRAY_WIDGET_X = 200; | ||
|
||
Codec<IRemoteWidget> CODEC = PNCRegistries.REMOTE_WIDGETS_REGISTRY.byNameCodec().dispatch( | ||
IRemoteWidget::getType, | ||
RemoteWidgetType::codec | ||
); | ||
|
||
StreamCodec<RegistryFriendlyByteBuf,IRemoteWidget> STREAM_CODEC | ||
= ByteBufCodecs.registry(PNCRegistries.REMOTE_WIDGETS_KEY) | ||
.dispatch(IRemoteWidget::getType, RemoteWidgetType::streamCodec); | ||
|
||
/** | ||
* {@return Base settings, common to all remote widgets} | ||
*/ | ||
BaseSettings baseSettings(); | ||
|
||
/** | ||
* {@return Settings for the physical Minecraft widget, common to all remote widgets} | ||
*/ | ||
WidgetSettings widgetSettings(); | ||
|
||
/** | ||
* {@return an exact copy of this remote widget} | ||
*/ | ||
@ApiStatus.NonExtendable | ||
default IRemoteWidget copy() { | ||
return copyToPos(widgetSettings().x(), widgetSettings().y()); | ||
} | ||
|
||
/** | ||
* Make a copy of this remote widget, at the new X/Y physical widget position | ||
* @param x X | ||
* @param y Y | ||
* @return the copied remote widget | ||
*/ | ||
IRemoteWidget copyToPos(int x, int y); | ||
|
||
/** | ||
* Does this widget allow configuration of its title and tooltip? | ||
* @return true if configurable, false if not | ||
*/ | ||
default boolean hasConfigurableText() { | ||
return true; | ||
} | ||
|
||
/** | ||
* {@return the widget type, which handles serialization} | ||
*/ | ||
RemoteWidgetType<? extends IRemoteWidget> getType(); | ||
|
||
default String getTranslationKey() { | ||
return getTranslationKey(getType()); | ||
} | ||
|
||
static String getTranslationKey(RemoteWidgetType<?> type) { | ||
String id = PNCRegistries.REMOTE_WIDGETS_REGISTRY.getResourceKey(type) | ||
.map(key -> key.location().toLanguageKey()) | ||
.orElse("unknown"); | ||
return "pneumaticcraft.gui.remote.tray." + id + ".name"; | ||
} | ||
|
||
static String getTooltipTranslationKey(RemoteWidgetType<?> type) { | ||
String id = PNCRegistries.REMOTE_WIDGETS_REGISTRY.getResourceKey(type) | ||
.map(key -> key.location().toLanguageKey()) | ||
.orElse("unknown"); | ||
return "pneumaticcraft.gui.remote.tray." + id + ".tooltip"; | ||
} | ||
|
||
default void discoverVariables(Set<String> variables, UUID playerId) { | ||
if (!baseSettings().enableVariable().isEmpty()) { | ||
variables.add(baseSettings().enableVariable()); | ||
} | ||
if (this instanceof IRemoteVariableWidget v && !v.varName().isEmpty()) { | ||
variables.add(v.varName()); | ||
} | ||
IGlobalVariableHelper helper = PneumaticRegistry.getInstance().getMiscHelpers().getGlobalVariableHelper(); | ||
widgetSettings().title().visit(string -> { | ||
variables.addAll(helper.getRelevantVariables(string, playerId)); | ||
return Optional.empty(); | ||
}); | ||
} | ||
|
||
default boolean isEnabled(Player player) { | ||
if (baseSettings().enableVariable().isEmpty()) { | ||
return true; | ||
} | ||
BlockPos pos = PneumaticRegistry.getInstance().getMiscHelpers().getGlobalVariableHelper() | ||
.getPos(player.getUUID(), baseSettings().enableVariable(), BlockPos.ZERO); | ||
return pos.equals(baseSettings().enablingValue()); | ||
} | ||
} |
Oops, something went wrong.