Add weather world properties

Dieser Commit ist enthalten in:
Moulberry 2023-09-21 18:13:57 +08:00
Ursprung 59cf7b49b9
Commit 8b107c3a9a
4 geänderte Dateien mit 72 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -11,6 +11,7 @@ import com.moulberry.axiom.world_properties.server.ServerWorldProperty;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.papermc.paper.event.player.PlayerFailMoveEvent;
import io.papermc.paper.event.world.WorldGameRuleChangeEvent;
import io.papermc.paper.network.ChannelInitializeListener;
import io.papermc.paper.network.ChannelInitializeListenerHolder;
import net.kyori.adventure.key.Key;
@ -21,6 +22,7 @@ import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.GameRules;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -163,8 +165,21 @@ public class AxiomPaper extends JavaPlugin implements Listener {
}, 20); // Why does this need to be delayed?
}
@EventHandler
public void onGameRuleChanged(WorldGameRuleChangeEvent event) {
if (event.getGameRule() == GameRule.DO_WEATHER_CYCLE) {
ServerWorldPropertiesRegistry properties = getWorldProperties(event.getWorld());
if (properties != null) {
ServerWorldProperty<?> property = properties.getById(new ResourceLocation("axiom:pause_weather"));
if (property != null) {
((ServerWorldProperty<Boolean>)property).setValue(event.getWorld(), Boolean.valueOf(event.getValue()));
}
}
}
}
private ServerWorldPropertiesRegistry createWorldProperties(World world) {
ServerWorldPropertiesRegistry registry = new ServerWorldPropertiesRegistry();
ServerWorldPropertiesRegistry registry = new ServerWorldPropertiesRegistry(world);
AxiomCreateWorldPropertiesEvent createEvent = new AxiomCreateWorldPropertiesEvent(world, registry);
Bukkit.getPluginManager().callEvent(createEvent);

Datei anzeigen

@ -4,6 +4,8 @@ import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Unit;
import java.util.List;
public interface WorldPropertyWidgetType<T> {
WorldPropertyDataType<T> dataType();
@ -72,4 +74,17 @@ public interface WorldPropertyWidgetType<T> {
}
};
record ButtonArray(List<String> otherButtons) implements WorldPropertyWidgetType<Integer> {
@Override
public WorldPropertyDataType<Integer> dataType() {
return WorldPropertyDataType.INTEGER;
}
@Override
public void write(FriendlyByteBuf friendlyByteBuf) {
friendlyByteBuf.writeVarInt(5);
friendlyByteBuf.writeCollection(this.otherButtons, FriendlyByteBuf::writeUtf);
}
}
}

Datei anzeigen

@ -6,6 +6,9 @@ import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.GameRules;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -19,8 +22,8 @@ public class ServerWorldPropertiesRegistry {
private final LinkedHashMap<WorldPropertyCategory, List<ServerWorldProperty<?>>> propertyList = new LinkedHashMap<>();
private final Map<ResourceLocation, ServerWorldProperty<?>> propertyMap = new HashMap<>();
public ServerWorldPropertiesRegistry() {
this.registerDefault();
public ServerWorldPropertiesRegistry(World world) {
this.registerDefault(world);
}
public ServerWorldProperty<?> getById(ResourceLocation resourceLocation) {
@ -53,7 +56,10 @@ public class ServerWorldPropertiesRegistry {
buf.accessByteBufWithCorrectSize());
}
public void registerDefault() {
public void registerDefault(World world) {
ServerLevel serverLevel = ((CraftWorld)world).getHandle();
GameRules gameRules = serverLevel.getGameRules();
// Time
WorldPropertyCategory timeCategory = new WorldPropertyCategory("axiom.editorui.window.world_properties.time", true);
@ -63,6 +69,34 @@ public class ServerWorldPropertiesRegistry {
);
this.addCategory(timeCategory, List.of(time));
// Weather
WorldPropertyCategory weatherCategory = new WorldPropertyCategory("axiom.editorui.window.world_properties.weather",
true);
ServerWorldProperty<Boolean> pauseWeather = new ServerWorldProperty<>(new ResourceLocation("axiom:pause_weather"),
"axiom.editorui.window.world_properties.pause_weather",
true, WorldPropertyWidgetType.CHECKBOX, !gameRules.getRule(GameRules.RULE_WEATHER_CYCLE).get(), bool -> {
gameRules.getRule(GameRules.RULE_WEATHER_CYCLE).set(!bool, serverLevel);
return false;
});
ServerWorldProperty<Integer> weatherType = new ServerWorldProperty<>(new ResourceLocation("axiom:weather_type"),
"axiom.editorui.window.world_properties.clear_weather",
true, new WorldPropertyWidgetType.ButtonArray(
List.of("axiom.editorui.window.world_properties.rain_weather", "axiom.editorui.window.world_properties.thunder_weather")
), 0, index -> {
if (index == 0) {
serverLevel.setWeatherParameters(ServerLevel.RAIN_DELAY.sample(serverLevel.random), 0, false, false);
} else if (index == 1) {
serverLevel.setWeatherParameters(0, ServerLevel.RAIN_DURATION.sample(serverLevel.random), true, false);
} else if (index == 2) {
serverLevel.setWeatherParameters(0, ServerLevel.THUNDER_DURATION.sample(serverLevel.random), true, true);
}
return false;
});
this.addCategory(weatherCategory, List.of(pauseWeather, weatherType));
}
}

Datei anzeigen

@ -1,7 +1,6 @@
package com.moulberry.axiom.world_properties.server;
import com.moulberry.axiom.AxiomPaper;
import com.moulberry.axiom.world_properties.WorldPropertyCategory;
import com.moulberry.axiom.world_properties.WorldPropertyDataType;
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
import io.netty.buffer.Unpooled;
@ -51,8 +50,10 @@ public class ServerWorldProperty<T> {
}
public void setValue(World world, T value) {
this.value = value;
this.sync(world);
if (!value.equals(this.value)) {
this.value = value;
this.sync(world);
}
}
public void sync(World world) {