-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Op login hint in the next start after restoring
- Loading branch information
Showing
10 changed files
with
158 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/keuin/kbackupfabric/event/OnPlayerConnect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/keuin/kbackupfabric/mixin/PlayerManagerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/keuin/kbackupfabric/notification/DistinctNotifiable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/keuin/kbackupfabric/notification/NotificationManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(")")) | ||
); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
] | ||
}, | ||
"mixins": [ | ||
"kbackupfabric.mixins.json" | ||
], | ||
"depends": { | ||
"fabricloader": ">=0.7.4", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |