First commit
This commit is contained in:
30
src/main/java/com/micle/loginprotection/LoginProtection.java
Executable file
30
src/main/java/com/micle/loginprotection/LoginProtection.java
Executable file
@ -0,0 +1,30 @@
|
||||
package com.micle.loginprotection;
|
||||
|
||||
import com.micle.loginprotection.data.ProtectedPlayers;
|
||||
import com.micle.loginprotection.setup.Registration;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fmllegacy.network.NetworkRegistry;
|
||||
import net.minecraftforge.fmllegacy.network.simple.SimpleChannel;
|
||||
|
||||
@Mod(LoginProtection.MOD_ID)
|
||||
public class LoginProtection {
|
||||
public static final String MOD_ID = "loginprotection";
|
||||
public static ProtectedPlayers protected_players = new ProtectedPlayers();
|
||||
public static boolean has_pressed_key = false;
|
||||
|
||||
private static final String PROTOCOL_VERSION = "1";
|
||||
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
|
||||
new ResourceLocation(LoginProtection.MOD_ID, "main"),
|
||||
() -> PROTOCOL_VERSION,
|
||||
PROTOCOL_VERSION::equals,
|
||||
PROTOCOL_VERSION::equals
|
||||
);
|
||||
|
||||
public LoginProtection() {
|
||||
Registration.register();
|
||||
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
}
|
||||
35
src/main/java/com/micle/loginprotection/data/ProtectedPlayer.java
Executable file
35
src/main/java/com/micle/loginprotection/data/ProtectedPlayer.java
Executable file
@ -0,0 +1,35 @@
|
||||
package com.micle.loginprotection.data;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class ProtectedPlayer {
|
||||
private final UUID player_uuid;
|
||||
private int grace_period;
|
||||
private boolean is_loading;
|
||||
|
||||
public ProtectedPlayer(UUID player_uuid) {
|
||||
this.player_uuid = player_uuid;
|
||||
this.grace_period = 400;
|
||||
this.is_loading = true;
|
||||
}
|
||||
|
||||
public UUID getPlayerUUID() {
|
||||
return this.player_uuid;
|
||||
}
|
||||
|
||||
public int getGracePeriod() {
|
||||
return this.grace_period;
|
||||
}
|
||||
|
||||
public void setGracePeriod(int new_grace_period) {
|
||||
this.grace_period = new_grace_period;
|
||||
}
|
||||
|
||||
public boolean isLoading() {
|
||||
return this.is_loading;
|
||||
}
|
||||
|
||||
public void setLoading(boolean new_loading) {
|
||||
this.is_loading = new_loading;
|
||||
}
|
||||
}
|
||||
56
src/main/java/com/micle/loginprotection/data/ProtectedPlayers.java
Executable file
56
src/main/java/com/micle/loginprotection/data/ProtectedPlayers.java
Executable file
@ -0,0 +1,56 @@
|
||||
package com.micle.loginprotection.data;
|
||||
|
||||
import net.minecraft.network.chat.TextComponent;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraftforge.fmllegacy.server.ServerLifecycleHooks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ProtectedPlayers {
|
||||
private final List<ProtectedPlayer> protected_players = new ArrayList<>();
|
||||
|
||||
public ProtectedPlayers() { }
|
||||
|
||||
public int size() {
|
||||
return protected_players.size();
|
||||
}
|
||||
|
||||
public void addPlayer(UUID player_uuid) {
|
||||
protected_players.add(new ProtectedPlayer(player_uuid));
|
||||
}
|
||||
|
||||
public ProtectedPlayer getPlayer(UUID player_uuid) {
|
||||
ProtectedPlayer player;
|
||||
for (ProtectedPlayer protected_player : protected_players) {
|
||||
player = protected_player;
|
||||
if (player.getPlayerUUID() == player_uuid) {
|
||||
return player;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void removePlayer(UUID player_uuid) {
|
||||
ProtectedPlayer protected_player = getPlayer(player_uuid);
|
||||
if (protected_player == null) { return; }
|
||||
protected_players.remove(protected_player);
|
||||
|
||||
ServerPlayer player = ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayer(player_uuid);
|
||||
if (player == null) { return; }
|
||||
player.sendMessage(new TextComponent("Grace Period over!"), player_uuid);
|
||||
}
|
||||
|
||||
public void updateGracePeriod(UUID player_uuid) {
|
||||
ProtectedPlayer protected_player = getPlayer(player_uuid);
|
||||
if (protected_player.isLoading()) { return; }
|
||||
|
||||
int grace_period = protected_player.getGracePeriod()-1;
|
||||
protected_player.setGracePeriod(grace_period);
|
||||
if (grace_period <= 0) {
|
||||
removePlayer(player_uuid);
|
||||
System.out.println(protected_player.getPlayerUUID() + " is no longer being protected!");
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java
Executable file
32
src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java
Executable file
@ -0,0 +1,32 @@
|
||||
package com.micle.loginprotection.events;
|
||||
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import com.micle.loginprotection.network.C2SKeyPress;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.ServerData;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.client.event.InputEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
public class OnKeyPressEventHandler {
|
||||
@SubscribeEvent
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public void KeyPressEvent(InputEvent.KeyInputEvent event) {
|
||||
ServerData server = Minecraft.getInstance().getCurrentServer();
|
||||
if (server == null) { return; }
|
||||
try {
|
||||
LoginProtection.INSTANCE.sendToServer(new C2SKeyPress());
|
||||
} catch (NullPointerException ignored) { }
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public void MouseClickEvent(InputEvent.ClickInputEvent event) {
|
||||
ServerData server = Minecraft.getInstance().getCurrentServer();
|
||||
if (server == null) { return; }
|
||||
try {
|
||||
LoginProtection.INSTANCE.sendToServer(new C2SKeyPress());
|
||||
} catch (NullPointerException ignored) { }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.micle.loginprotection.events;
|
||||
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraftforge.event.entity.living.LivingDamageEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
public class OnPlayerDamageEventHandler {
|
||||
@SubscribeEvent
|
||||
public void LivingDamageEvent(LivingDamageEvent event) {
|
||||
if (!(event.getEntity() instanceof Player)) { return; }
|
||||
Player player = (Player) event.getEntity();
|
||||
|
||||
if (LoginProtection.protected_players.getPlayer(player.getUUID()) != null) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/main/java/com/micle/loginprotection/events/OnPlayerJoinEventHandler.java
Executable file
20
src/main/java/com/micle/loginprotection/events/OnPlayerJoinEventHandler.java
Executable file
@ -0,0 +1,20 @@
|
||||
package com.micle.loginprotection.events;
|
||||
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
public class OnPlayerJoinEventHandler {
|
||||
@SubscribeEvent
|
||||
@OnlyIn(Dist.DEDICATED_SERVER)
|
||||
public void EntityJoinWorldEvent(PlayerEvent.PlayerLoggedInEvent event) {
|
||||
if (!(event.getEntity() instanceof Player)) { return; }
|
||||
Player player = (Player) event.getEntity();
|
||||
|
||||
LoginProtection.protected_players.addPlayer(player.getUUID());
|
||||
System.out.println(player.getUUID() + " (" + player.getDisplayName().getString() + ") is being protected!");
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package com.micle.loginprotection.events;
|
||||
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
public class OnPlayerLeaveEventHandler {
|
||||
@SubscribeEvent
|
||||
@OnlyIn(Dist.DEDICATED_SERVER)
|
||||
public void PlayerLeaveEvent(PlayerEvent.PlayerLoggedOutEvent event) {
|
||||
if (!(event.getEntity() instanceof Player)) { return; }
|
||||
Player player = event.getPlayer();
|
||||
|
||||
LoginProtection.protected_players.removePlayer(player.getUUID());
|
||||
}
|
||||
}
|
||||
14
src/main/java/com/micle/loginprotection/events/OnPlayerTickEventHandler.java
Executable file
14
src/main/java/com/micle/loginprotection/events/OnPlayerTickEventHandler.java
Executable file
@ -0,0 +1,14 @@
|
||||
package com.micle.loginprotection.events;
|
||||
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import net.minecraftforge.event.TickEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
public class OnPlayerTickEventHandler {
|
||||
@SubscribeEvent
|
||||
public void PlayerTickEvent(TickEvent.PlayerTickEvent event) {
|
||||
if (LoginProtection.protected_players.getPlayer(event.player.getUUID()) == null) { return; }
|
||||
|
||||
LoginProtection.protected_players.updateGracePeriod(event.player.getUUID());
|
||||
}
|
||||
}
|
||||
36
src/main/java/com/micle/loginprotection/network/C2SKeyPress.java
Executable file
36
src/main/java/com/micle/loginprotection/network/C2SKeyPress.java
Executable file
@ -0,0 +1,36 @@
|
||||
package com.micle.loginprotection.network;
|
||||
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.chat.TextComponent;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraftforge.fmllegacy.network.NetworkEvent;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class C2SKeyPress {
|
||||
public C2SKeyPress() { }
|
||||
|
||||
public static void encode(final C2SKeyPress msg, final FriendlyByteBuf packet_buffer) { }
|
||||
|
||||
public static C2SKeyPress decode(final FriendlyByteBuf packet_buffer) {
|
||||
return new C2SKeyPress();
|
||||
}
|
||||
|
||||
public static void handle(final C2SKeyPress msg, final Supplier<NetworkEvent.Context> context_supplier) {
|
||||
final NetworkEvent.Context context = context_supplier.get();
|
||||
context.enqueueWork(() -> {
|
||||
final ServerPlayer sender = context.getSender();
|
||||
if (sender == null) { return; }
|
||||
if (LoginProtection.protected_players.getPlayer(sender.getUUID()) == null) { return; }
|
||||
if (!LoginProtection.protected_players.getPlayer(sender.getUUID()).isLoading()) { return; }
|
||||
|
||||
LoginProtection.protected_players.getPlayer(sender.getUUID()).setLoading(false);
|
||||
sender.sendMessage(new TextComponent("Grace Period started!"), sender.getUUID());
|
||||
if (sender.isInWater()) {
|
||||
sender.setAirSupply(sender.getMaxAirSupply());
|
||||
}
|
||||
});
|
||||
context.setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
28
src/main/java/com/micle/loginprotection/setup/Registration.java
Executable file
28
src/main/java/com/micle/loginprotection/setup/Registration.java
Executable file
@ -0,0 +1,28 @@
|
||||
package com.micle.loginprotection.setup;
|
||||
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import com.micle.loginprotection.events.*;
|
||||
import com.micle.loginprotection.network.C2SKeyPress;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
|
||||
public class Registration {
|
||||
public static void register() {
|
||||
IEventBus mod_event_bus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
|
||||
MinecraftForge.EVENT_BUS.register(new OnPlayerJoinEventHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new OnPlayerDamageEventHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new OnPlayerTickEventHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new OnKeyPressEventHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new OnPlayerLeaveEventHandler());
|
||||
|
||||
int id = 0;
|
||||
LoginProtection.INSTANCE.registerMessage(id++,
|
||||
C2SKeyPress.class,
|
||||
C2SKeyPress::encode,
|
||||
C2SKeyPress::decode,
|
||||
C2SKeyPress::handle
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user