mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-04-06 18:10:29 +08:00
Replace Gamemode mixin with the appropriate listener
This commit is contained in:
parent
80065f1173
commit
10bcd70cd9
@ -21,13 +21,6 @@ dependencies {
|
||||
implementation project(path: ":extensions:adventure", configuration: "shadow")
|
||||
}
|
||||
|
||||
jar {
|
||||
// Add the sponge mixin into the manifest
|
||||
manifest.attributes([
|
||||
"MixinConfigs": "plan-sponge.mixins.json"
|
||||
])
|
||||
}
|
||||
|
||||
tasks.named("shadowJar", ShadowJar) {
|
||||
// Exclude these files
|
||||
exclude "**/*.svg"
|
||||
|
@ -22,23 +22,21 @@ import com.djrapitops.plan.identification.ServerInfo;
|
||||
import com.djrapitops.plan.settings.config.WorldAliasSettings;
|
||||
import com.djrapitops.plan.storage.database.DBSystem;
|
||||
import com.djrapitops.plan.storage.database.transactions.events.StoreWorldNameTransaction;
|
||||
import com.djrapitops.plan.utilities.logging.ErrorContext;
|
||||
import com.djrapitops.plan.utilities.logging.ErrorLogger;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.GameType;
|
||||
import org.spongepowered.api.ResourceKey;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.data.DataTransactionResult;
|
||||
import org.spongepowered.api.data.Keys;
|
||||
import org.spongepowered.api.data.value.Value;
|
||||
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
|
||||
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.Order;
|
||||
import org.spongepowered.api.event.data.ChangeDataHolderEvent;
|
||||
import org.spongepowered.api.registry.RegistryTypes;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Listener for GameMode change on Sponge.
|
||||
@ -47,8 +45,6 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public class SpongeGMChangeListener {
|
||||
|
||||
public static final List<Consumer<Event>> EVENT_CONSUMERS = new ArrayList<>(); // Available to the mixin
|
||||
|
||||
private final WorldAliasSettings worldAliasSettings;
|
||||
private final ServerInfo serverInfo;
|
||||
private final DBSystem dbSystem;
|
||||
@ -65,59 +61,25 @@ public class SpongeGMChangeListener {
|
||||
this.serverInfo = serverInfo;
|
||||
this.dbSystem = dbSystem;
|
||||
this.errorLogger = errorLogger;
|
||||
EVENT_CONSUMERS.add(this::onMixin);
|
||||
}
|
||||
|
||||
public static class Event {
|
||||
private final Player player;
|
||||
private final GameType gameType;
|
||||
|
||||
public Event(Player player, GameType gameType) {
|
||||
this.player = player;
|
||||
this.gameType = gameType;
|
||||
}
|
||||
}
|
||||
|
||||
private void onMixin(Event event) {
|
||||
ServerPlayer serverPlayer = Sponge.game().server()
|
||||
.player(event.player.getUUID())
|
||||
.orElse(null);
|
||||
if (serverPlayer == null) {
|
||||
// uh oh
|
||||
errorLogger.error(
|
||||
new RuntimeException("GameMode changed for player but no ServerPlayer was found"),
|
||||
ErrorContext.builder()
|
||||
.related(event.player, event.player.getGameProfile().getName())
|
||||
.whatToDo("Report this, the gamemode change mixin might be broken")
|
||||
.build()
|
||||
);
|
||||
@Listener(order = Order.POST)
|
||||
public void onGMChange(ChangeDataHolderEvent.ValueChange event) {
|
||||
ServerPlayer player = event.targetHolder() instanceof ServerPlayer serverPlayer ? serverPlayer : null;
|
||||
if (player == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
GameMode gameMode = GameModes.registry().value(ResourceKey.sponge(event.gameType.getName()));
|
||||
actOnGMChangeEvent(serverPlayer, gameMode);
|
||||
DataTransactionResult result = event.endResult();
|
||||
Optional<Value.Immutable<GameMode>> gameModeValue = result.successfulValue(Keys.GAME_MODE);
|
||||
if (gameModeValue.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
GameMode newMode = gameModeValue.get().get();
|
||||
actOnGMChangeEvent(player, newMode);
|
||||
}
|
||||
|
||||
// This listener can replace the mixin if this pr is merged:
|
||||
// https://github.com/SpongePowered/Sponge/pull/3563
|
||||
|
||||
// @Listener(order = Order.POST)
|
||||
// public void onGMChange(ChangeDataHolderEvent.ValueChange event) {
|
||||
// ServerPlayer player = event.targetHolder() instanceof ServerPlayer ? (ServerPlayer) event.targetHolder() : null;
|
||||
// if (player == null) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// DataTransactionResult result = event.endResult();
|
||||
// Optional<Value.Immutable<GameMode>> gameModeValue = result.successfulValue(Keys.GAME_MODE);
|
||||
// if (gameModeValue.isEmpty()) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// GameMode newMode = gameModeValue.get().get();
|
||||
// actOnGMChangeEvent(player, newMode);
|
||||
// }
|
||||
|
||||
private void actOnGMChangeEvent(ServerPlayer player, GameMode gameMode) {
|
||||
UUID uuid = player.uniqueId();
|
||||
long time = System.currentTimeMillis();
|
||||
|
@ -1,36 +0,0 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.gathering.mixin;
|
||||
|
||||
import com.djrapitops.plan.gathering.listeners.sponge.SpongeGMChangeListener;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.GameType;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(ServerPlayer.class)
|
||||
public class GameModeChangeMixin {
|
||||
|
||||
@Inject(method = "setGameMode", at = @At("HEAD"))
|
||||
private void onGameModeChange(GameType gameType, CallbackInfo ci) {
|
||||
SpongeGMChangeListener.Event event = new SpongeGMChangeListener.Event((Player) (Object) this, gameType);
|
||||
SpongeGMChangeListener.EVENT_CONSUMERS.forEach(consumer -> consumer.accept(event));
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
{
|
||||
"required": true,
|
||||
"package": "com.djrapitops.plan.gathering.mixin",
|
||||
"minVersion": "0.7.11",
|
||||
"mixins": [
|
||||
"GameModeChangeMixin"
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user