Skip to content

Commit

Permalink
Update Fabric to MC 1.21.2 (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD authored Oct 22, 2024
1 parent 4face76 commit aa667a5
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ public AbstractCommandSender(S delegate) {
this.delegate = delegate;
}

protected Object getObjectForComparison() {
return this.delegate;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractCommandSender<?> that = (AbstractCommandSender<?>) o;
return this.delegate.equals(that.delegate);
return this.getObjectForComparison().equals(that.getObjectForComparison());
}

@Override
public int hashCode() {
return this.delegate.hashCode();
return this.getObjectForComparison().hashCode();
}
}
12 changes: 6 additions & 6 deletions spark-fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ configurations {

dependencies {
// https://modmuss50.me/fabric.html
minecraft 'com.mojang:minecraft:1.21.1'
mappings 'net.fabricmc:yarn:1.21.1+build.3:v2'
modImplementation 'net.fabricmc:fabric-loader:0.15.11'
minecraft 'com.mojang:minecraft:1.21.2'
mappings 'net.fabricmc:yarn:1.21.2+build.1:v2'
modImplementation 'net.fabricmc:fabric-loader:0.16.7'

Set<String> apiModules = [
"fabric-api-base",
Expand All @@ -40,12 +40,12 @@ dependencies {

// Add each module as a dependency
apiModules.forEach {
modImplementation(fabricApi.module(it, '0.102.1+1.21.1'))
modImplementation(fabricApi.module(it, '0.106.1+1.21.2'))
}

include(modImplementation('me.lucko:fabric-permissions-api:0.3.1'))
include(modImplementation('me.lucko:fabric-permissions-api:0.3.2'))

modImplementation('eu.pb4:placeholder-api:2.4.1+1.21')
modImplementation('eu.pb4:placeholder-api:2.5.0+1.21.2')

shade project(':spark-common')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,47 @@
package me.lucko.spark.fabric;

import me.lucko.spark.common.command.sender.AbstractCommandSender;
import me.lucko.spark.fabric.plugin.FabricSparkPlugin;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.client.network.ClientCommandSource;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandOutput;
import net.minecraft.server.rcon.RconCommandOutput;
import net.minecraft.text.Text;

import java.util.UUID;

public class FabricCommandSender extends AbstractCommandSender<CommandOutput> {
private final FabricSparkPlugin plugin;
public class FabricClientCommandSender extends AbstractCommandSender<FabricClientCommandSource> {
public FabricClientCommandSender(FabricClientCommandSource commandSource) {
super(commandSource);
}

public FabricCommandSender(CommandOutput commandOutput, FabricSparkPlugin plugin) {
super(commandOutput);
this.plugin = plugin;
public FabricClientCommandSender(ClientCommandSource commandSource) {
this((FabricClientCommandSource) commandSource);
}

@Override
public String getName() {
if (super.delegate instanceof PlayerEntity) {
return ((PlayerEntity) super.delegate).getGameProfile().getName();
} else if (super.delegate instanceof MinecraftServer) {
return "Console";
} else if (super.delegate instanceof RconCommandOutput) {
return "RCON Console";
} else {
return "unknown:" + super.delegate.getClass().getSimpleName();
}
return this.delegate.getPlayer().getGameProfile().getName();
}

@Override
public UUID getUniqueId() {
if (super.delegate instanceof PlayerEntity) {
return ((PlayerEntity) super.delegate).getUuid();
}
return null;
return this.delegate.getPlayer().getUuid();
}

@Override
public void sendMessage(Component message) {
Text component = Text.Serialization.fromJsonTree(GsonComponentSerializer.gson().serializeToTree(message), DynamicRegistryManager.EMPTY);
super.delegate.sendMessage(component);
this.delegate.sendFeedback(component);
}

@Override
public boolean hasPermission(String permission) {
return this.plugin.hasPermission(super.delegate, permission);
return true;
}

@Override
protected Object getObjectForComparison() {
return this.delegate.getPlayer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* This file is part of spark.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* 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
* (at your option) 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 <http://www.gnu.org/licenses/>.
*/

package me.lucko.spark.fabric;

import me.lucko.fabric.api.permissions.v0.Permissions;
import me.lucko.spark.common.command.sender.AbstractCommandSender;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.entity.Entity;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

import java.util.UUID;

public class FabricServerCommandSender extends AbstractCommandSender<ServerCommandSource> {
public FabricServerCommandSender(ServerCommandSource commandSource) {
super(commandSource);
}

@Override
public String getName() {
String name = this.delegate.getName();
if (this.delegate.getEntity() != null && name.equals("Server")) {
return "Console";
}
return name;
}

@Override
public UUID getUniqueId() {
Entity entity = this.delegate.getEntity();
return entity != null ? entity.getUuid() : null;
}

@Override
public void sendMessage(Component message) {
Text component = Text.Serialization.fromJsonTree(GsonComponentSerializer.gson().serializeToTree(message), DynamicRegistryManager.EMPTY);
this.delegate.sendMessage(component);
}

@Override
public boolean hasPermission(String permission) {
return Permissions.getPermissionValue(this.delegate, permission).orElseGet(() -> {
ServerPlayerEntity player = this.delegate.getPlayer();
MinecraftServer server = this.delegate.getServer();
if (player != null) {
if (server != null && server.isHost(player.getGameProfile())) {
return true;
}
return player.hasPermissionLevel(4);
}
return true;
});
}

@Override
protected Object getObjectForComparison() {
UUID uniqueId = getUniqueId();
if (uniqueId != null) {
return uniqueId;
}
Entity entity = this.delegate.getEntity();
if (entity != null) {
return entity;
}
return getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,19 @@ public GameRulesResult pollGameRules() {
GameRulesResult data = new GameRulesResult();
Iterable<ServerWorld> worlds = this.server.getWorlds();

GameRules.accept(new GameRules.Visitor() {
@Override
public <T extends GameRules.Rule<T>> void visit(GameRules.Key<T> key, GameRules.Type<T> type) {
String defaultValue = type.createRule().serialize();
data.putDefault(key.getName(), defaultValue);
for (ServerWorld world : worlds) {
world.getGameRules().accept(new GameRules.Visitor() {
@Override
public <T extends GameRules.Rule<T>> void visit(GameRules.Key<T> key, GameRules.Type<T> type) {
String defaultValue = type.createRule().serialize();
data.putDefault(key.getName(), defaultValue);

for (ServerWorld world : worlds) {
String worldName = world.getRegistryKey().getValue().getPath();

String value = world.getGameRules().get(key).serialize();
data.put(key.getName(), worldName, value);
}
}
});
});
}
return data;
}

Expand Down Expand Up @@ -208,28 +207,8 @@ public ChunksResult<FabricChunkInfo> pollChunks() {

@Override
public GameRulesResult pollGameRules() {
ClientWorld world = this.client.world;
if (world == null) {
return null;
}

GameRulesResult data = new GameRulesResult();

String worldName = world.getRegistryKey().getValue().getPath();
GameRules worldRules = world.getGameRules();

GameRules.accept(new GameRules.Visitor() {
@Override
public <T extends GameRules.Rule<T>> void visit(GameRules.Key<T> key, GameRules.Type<T> type) {
String defaultValue = type.createRule().serialize();
data.putDefault(key.getName(), defaultValue);

String value = worldRules.get(key).serialize();
data.put(key.getName(), worldName, value);
}
});

return data;
// Not available on client since 24w39a
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import me.lucko.spark.common.sampler.ThreadDumper;
import me.lucko.spark.common.tick.TickHook;
import me.lucko.spark.common.tick.TickReporter;
import me.lucko.spark.fabric.FabricCommandSender;
import me.lucko.spark.fabric.FabricClientCommandSender;
import me.lucko.spark.fabric.FabricPlatformInfo;
import me.lucko.spark.fabric.FabricSparkMod;
import me.lucko.spark.fabric.FabricTickHook;
Expand All @@ -43,8 +43,8 @@
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandOutput;

import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
Expand Down Expand Up @@ -91,7 +91,7 @@ public int run(CommandContext<FabricClientCommandSource> context) throws Command
return 0;
}

this.platform.executeCommand(new FabricCommandSender(context.getSource().getEntity(), this), args);
this.platform.executeCommand(new FabricClientCommandSender(context.getSource()), args);
return Command.SINGLE_SUCCESS;
}

Expand All @@ -102,17 +102,16 @@ public CompletableFuture<Suggestions> getSuggestions(CommandContext<FabricClient
return Suggestions.empty();
}

return generateSuggestions(new FabricCommandSender(context.getSource().getEntity(), this), args, builder);
return generateSuggestions(new FabricClientCommandSender(context.getSource()), args, builder);
}

@Override
public boolean hasPermission(CommandOutput sender, String permission) {
return true;
}

@Override
public Stream<FabricCommandSender> getCommandSenders() {
return Stream.of(new FabricCommandSender(this.minecraft.player, this));
public Stream<FabricClientCommandSender> getCommandSenders() {
ClientPlayNetworkHandler networkHandler = this.minecraft.getNetworkHandler();
if (networkHandler == null) {
return Stream.empty();
}
return Stream.of(new FabricClientCommandSender(networkHandler.getCommandSource()));
}

@Override
Expand Down
Loading

0 comments on commit aa667a5

Please sign in to comment.