3
0
Mirror von https://github.com/Moulberry/AxiomPaperPlugin.git synchronisiert 2024-11-08 17:40:04 +01:00

WorldProperties: Create more easily extendable ServerWorldPropertyBase class

Dieser Commit ist enthalten in:
Moulberry 2023-11-16 19:26:13 +08:00
Ursprung 89e04ffb39
Commit 8a17605969
5 geänderte Dateien mit 85 neuen und 54 gelöschten Zeilen

Datei anzeigen

@ -3,6 +3,7 @@ package com.moulberry.axiom.event;
import com.moulberry.axiom.world_properties.WorldPropertyCategory;
import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry;
import com.moulberry.axiom.world_properties.server.ServerWorldProperty;
import com.moulberry.axiom.world_properties.server.ServerWorldPropertyBase;
import com.moulberry.axiom.world_properties.server.ServerWorldPropertyHolder;
import org.bukkit.World;
import org.bukkit.event.Cancellable;
@ -29,7 +30,7 @@ public class AxiomCreateWorldPropertiesEvent extends Event implements Cancellabl
return world;
}
public void addCategory(WorldPropertyCategory category, List<ServerWorldProperty<?>> properties) {
public void addCategory(WorldPropertyCategory category, List<ServerWorldPropertyBase<?>> properties) {
this.registry.addCategory(category, properties);
}

Datei anzeigen

@ -31,11 +31,11 @@ public class ServerWorldPropertiesRegistry {
}
@SuppressWarnings("unchecked")
public void addCategory(WorldPropertyCategory category, List<ServerWorldProperty<?>> properties) {
public void addCategory(WorldPropertyCategory category, List<ServerWorldPropertyBase<?>> properties) {
List<ServerWorldPropertyHolder<?>> holders = new ArrayList<>();
for (ServerWorldProperty<?> property : properties) {
Object defaultValue = property.defaultValueFunction.apply(this.world);
holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldProperty<Object>) property));
for (ServerWorldPropertyBase<?> property : properties) {
Object defaultValue = property.getDefaultValue(this.world);
holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldPropertyBase<Object>) property));
}
this.propertyList.put(category, holders);

Datei anzeigen

@ -1,67 +1,33 @@
package com.moulberry.axiom.world_properties.server;
import com.moulberry.axiom.AxiomPaper;
import com.moulberry.axiom.world_properties.PropertyUpdateHandler;
import com.moulberry.axiom.world_properties.WorldPropertyDataType;
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
import net.minecraft.resources.ResourceLocation;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_20_R2.util.CraftNamespacedKey;
import org.bukkit.entity.Player;
import java.util.function.Function;
public class ServerWorldProperty<T> {
public class ServerWorldProperty<T> extends ServerWorldPropertyBase<T> {
private final ResourceLocation id;
/*package-private*/ final String name;
/*package-private*/ final boolean localizeName;
/*package-private*/ WorldPropertyWidgetType<T> widget;
/*package-private*/ Function<World, T> defaultValueFunction;
/*package-private*/ PropertyUpdateHandler<T> handler;
private final Function<World, T> defaultValueFunction;
private final PropertyUpdateHandler<T> handler;
public ServerWorldProperty(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType<T> widget,
Function<World, T> defaultValueFunction, PropertyUpdateHandler<T> handler) {
this.id = CraftNamespacedKey.toMinecraft(id);
this.name = name;
this.localizeName = localizeName;
this.widget = widget;
super(id, name, localizeName, widget);
this.defaultValueFunction = defaultValueFunction;
this.handler = handler;
}
public ResourceLocation getId() {
return this.id;
@Override
public T getDefaultValue(World world) {
return this.defaultValueFunction.apply(world);
}
public WorldPropertyDataType<T> getType() {
return this.widget.dataType();
}
@SuppressWarnings("unchecked")
public boolean setValueWithoutSyncing(World world, T value) {
ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world);
if (properties != null) {
ServerWorldPropertyHolder<?> property = properties.getById(this.id);
if (property != null && property.getProperty() == this) {
((ServerWorldPropertyHolder<T>)property).setValueWithoutSyncing(value);
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
public boolean setValue(World world, T value) {
ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world);
if (properties != null) {
ServerWorldPropertyHolder<?> property = properties.getById(this.id);
if (property != null && property.getProperty() == this) {
((ServerWorldPropertyHolder<T>)property).setValue(world, value);
return true;
}
}
return false;
@Override
public boolean handleUpdateProperty(Player player, World world, T value) {
return this.handler.update(player, world, value);
}
}

Datei anzeigen

@ -0,0 +1,64 @@
package com.moulberry.axiom.world_properties.server;
import com.moulberry.axiom.AxiomPaper;
import com.moulberry.axiom.world_properties.WorldPropertyDataType;
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
import net.minecraft.resources.ResourceLocation;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_20_R2.util.CraftNamespacedKey;
import org.bukkit.entity.Player;
public abstract class ServerWorldPropertyBase<T> {
private final ResourceLocation id;
/*package-private*/ final String name;
/*package-private*/ final boolean localizeName;
/*package-private*/ WorldPropertyWidgetType<T> widget;
public ServerWorldPropertyBase(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType<T> widget) {
this.id = CraftNamespacedKey.toMinecraft(id);
this.name = name;
this.localizeName = localizeName;
this.widget = widget;
}
public abstract T getDefaultValue(World world);
public abstract boolean handleUpdateProperty(Player player, World world, T value);
public ResourceLocation getId() {
return this.id;
}
public WorldPropertyDataType<T> getType() {
return this.widget.dataType();
}
@SuppressWarnings("unchecked")
public boolean setValueWithoutSyncing(World world, T value) {
ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world);
if (properties != null) {
ServerWorldPropertyHolder<?> property = properties.getById(this.id);
if (property != null && property.getProperty() == this) {
((ServerWorldPropertyHolder<T>)property).setValueWithoutSyncing(value);
return true;
}
}
return false;
}
@SuppressWarnings("unchecked")
public boolean setValue(World world, T value) {
ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world);
if (properties != null) {
ServerWorldPropertyHolder<?> property = properties.getById(this.id);
if (property != null && property.getProperty() == this) {
((ServerWorldPropertyHolder<T>)property).setValue(world, value);
return true;
}
}
return false;
}
}

Datei anzeigen

@ -15,9 +15,9 @@ import org.bukkit.entity.Player;
public class ServerWorldPropertyHolder<T> {
private T value;
private ServerWorldProperty<T> property;
private ServerWorldPropertyBase<T> property;
public ServerWorldPropertyHolder(T value, ServerWorldProperty<T> property) {
public ServerWorldPropertyHolder(T value, ServerWorldPropertyBase<T> property) {
this.value = value;
this.property = property;
}
@ -30,13 +30,13 @@ public class ServerWorldPropertyHolder<T> {
return this.property.widget.dataType();
}
public ServerWorldProperty<T> getProperty() {
public ServerWorldPropertyBase<T> getProperty() {
return property;
}
public void update(Player player, World world, byte[] data) {
this.value = this.property.widget.dataType().deserialize(data);
if (this.property.handler.update(player, world, this.value)) {
if (this.property.handleUpdateProperty(player, world, this.value)) {
this.sync(world);
}
}