Skip to content

Commit

Permalink
Add Op login hint in the next start after restoring
Browse files Browse the repository at this point in the history
  • Loading branch information
keuin committed Jan 29, 2021
1 parent b020813 commit 602fb9e
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 4 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ done

## 3. To-Do List:

- Op login hint in the next start after restoring
- A more friendly help menu (colored command help menu)
- New version checker
- Code refactor for maintainability
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minecraft_version=1.14.4
yarn_mappings=1.14.4+build.18
loader_version=0.11.0
# Mod Properties
mod_version=1.6.2
mod_version=1.6.3
maven_group=com.keuin.kbackupfabric
archives_base_name=kbackup-fabric
# Dependencies
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.keuin.kbackupfabric.backup.BackupFilesystemUtil;
import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider;
import com.keuin.kbackupfabric.event.OnPlayerConnect;
import com.keuin.kbackupfabric.metadata.BackupMetadata;
import com.keuin.kbackupfabric.metadata.MetadataHolder;
import com.keuin.kbackupfabric.notification.DistinctNotifiable;
import com.keuin.kbackupfabric.notification.NotificationManager;
import com.keuin.kbackupfabric.ui.KBCommands;
import com.keuin.kbackupfabric.util.DateUtil;
import com.keuin.kbackupfabric.util.PrintUtil;
Expand Down Expand Up @@ -41,6 +44,11 @@ public void onStartServer(MinecraftServer server) {
if (!(server instanceof MinecraftDedicatedServer))
throw new RuntimeException("KBackup is a server-side-only plugin. Please do not use it in client-side.");

// Bind fabric events

OnPlayerConnect.ON_PLAYER_CONNECT.register((connection, player)
-> NotificationManager.INSTANCE.notifyPlayer(DistinctNotifiable.fromServerPlayerEntity(player)));

// Initialize player manager reference
PrintUtil.setPlayerManager(server.getPlayerManager());

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/keuin/kbackupfabric/event/OnPlayerConnect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.keuin.kbackupfabric.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.network.ServerPlayerEntity;

public class OnPlayerConnect {
public interface PlayerConnectEventCallback {
void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player);
}

public static final Event<PlayerConnectEventCallback> ON_PLAYER_CONNECT = EventFactory.createArrayBacked(PlayerConnectEventCallback.class, callbacks -> (conn, player) -> {
for (PlayerConnectEventCallback callback : callbacks) {
callback.onPlayerConnect(conn, player);
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.keuin.kbackupfabric.mixin;

import com.keuin.kbackupfabric.event.OnPlayerConnect;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
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(PlayerManager.class)
public class PlayerManagerMixin {
@Inject(method = "onPlayerConnect", at = @At("TAIL"))
public void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player, CallbackInfo ci) {
OnPlayerConnect.ON_PLAYER_CONNECT.invoker().onPlayerConnect(connection, player);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.keuin.kbackupfabric.notification;

import net.minecraft.network.MessageType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

/**
* Decouple from ServerPlayerEntity, in case further migration to other APIs.
*/
public interface DistinctNotifiable {

/**
* Does the receiver has privilege to receive some special message.
*/
boolean isPrivileged();

void notify(Text text);

/**
* Get an unique, non-null object that identifies this notifiable instance.
* The identifier must be immutable and implement their own equals method.
*
* @return the identifier.
*/
Object getIdentifier();

static DistinctNotifiable fromServerPlayerEntity(ServerPlayerEntity serverPlayerEntity) {
return new DistinctNotifiable() {
@Override
public boolean isPrivileged() {
return serverPlayerEntity.server.getPermissionLevel(serverPlayerEntity.getGameProfile()) >= serverPlayerEntity.server.getOpPermissionLevel();
}

@Override
public void notify(Text text) {
serverPlayerEntity.sendChatMessage(text, MessageType.SYSTEM);
}

@Override
public Object getIdentifier() {
return serverPlayerEntity.getUuid();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.keuin.kbackupfabric.notification;

import com.keuin.kbackupfabric.metadata.BackupMetadata;
import com.keuin.kbackupfabric.metadata.MetadataHolder;
import com.keuin.kbackupfabric.util.DateUtil;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Style;
import net.minecraft.util.Formatting;

import java.util.HashSet;
import java.util.Set;

/**
* Notify some users when the server has been restored to a backup.
*/
public class NotificationManager {

public static final NotificationManager INSTANCE = new NotificationManager();

private final Set<Object> notified = new HashSet<>();

private NotificationManager() {
}

public void notifyPlayer(DistinctNotifiable distinctNotifiable) {
Object identifier = distinctNotifiable.getIdentifier();
if (distinctNotifiable.isPrivileged() && !notified.contains(identifier)) {
notified.add(identifier);
notify(distinctNotifiable);
}
}

/**
* Just notify if necessary. It will not update the set.
*/
private void notify(DistinctNotifiable notifiable) {
if (MetadataHolder.hasMetadata()) {
BackupMetadata backup = MetadataHolder.getMetadata();
notifiable.notify(
new LiteralText("The server has been restored to backup ")
.append(new LiteralText("[" + backup.getBackupName() + "]").setStyle(new Style().setColor(Formatting.GREEN)))
.append(new LiteralText(" (created at "))
.append(new LiteralText("[" + DateUtil.fromEpochMillis(backup.getBackupTime()) + "]").setStyle(new Style().setColor(Formatting.GREEN)))
.append(new LiteralText(")"))
);
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/keuin/kbackupfabric/ui/KBCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public static int restore(CommandContext<ServerCommandSource> context) {
//KBMain.restore("name")
MinecraftServer server = context.getSource().getMinecraftServer();
String backupFileName = parseBackupFileName(context, StringArgumentType.getString(context, "backupName"));
backupFileName = parseBackupFileName(context, backupFileName);
// backupFileName = parseBackupFileName(context, backupFileName);

if (backupFileName == null)
return list(context); // Show the list and return
Expand Down Expand Up @@ -501,7 +501,7 @@ private static String parseBackupFileName(CommandContext<ServerCommandSource> co
String backupName = StringArgumentType.getString(context, "backupName");

if (backupName.matches("[0-9]*")) {
// If numeric input
// treat numeric input as backup index number in list
int index = Integer.parseInt(backupName) - 1;
synchronized (backupList) {
return backupList.get(index).getBackupFileName(); // Replace input number with real backup file name.
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
]
},
"mixins": [
"kbackupfabric.mixins.json"
],
"depends": {
"fabricloader": ">=0.7.4",
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/kbackupfabric.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"required": true,
"package": "com.keuin.kbackupfabric.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
"PlayerManagerMixin"
],
"client": [
],
"server": [
],
"injectors": {
"defaultRequire": 1
},
"verbose": true
}

0 comments on commit 602fb9e

Please sign in to comment.