Created a network packet for when a client presses a key. (Mod is now also client sided). Renamed some event handlers. Made it so after a client presses a key their grace period starts. After the grace period ends the player is removed from the list.

This commit is contained in:
micle
2021-06-03 17:01:28 +01:00
parent 21381e07af
commit 2688402c40
8 changed files with 105 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import java.util.List;
public class LoginProtection {
public static final String MOD_ID = "loginprotection";
public static ProtectedPlayers protected_players = new ProtectedPlayers();
public static boolean has_pressed_key = false;
public LoginProtection() {
Registration.register();

View File

@ -9,6 +9,10 @@ public class ProtectedPlayers {
public ProtectedPlayers() { }
public int size() {
return protected_players.size();
}
public void addPlayer(UUID player_uuid) {
protected_players.add(new ProtectedPlayer(player_uuid));
}
@ -23,4 +27,21 @@ public class ProtectedPlayers {
}
return player;
}
public void removePlayer(UUID player_uuid) {
ProtectedPlayer player = getPlayer(player_uuid);
if (player == null) { return; }
protected_players.remove(player);
}
public void updateGracePeriods() {
for (ProtectedPlayer protected_player : protected_players) {
if (protected_player.isLoading() || protected_player.getGracePeriod() <= 0) { continue; }
int grace_period = protected_player.getGracePeriod();
protected_player.setGracePeriod(grace_period-1);
if (grace_period <= 0) {
removePlayer(protected_player.getPlayerUUID());
}
}
}
}

View File

@ -0,0 +1,13 @@
package com.micle.loginprotection.events;
import com.micle.loginprotection.LoginProtection;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class OnKeyPressEventHandler {
@SubscribeEvent
public void KeyPressEvent(InputEvent.KeyInputEvent event) {
if (LoginProtection.has_pressed_key) { return; }
}
}

View File

@ -5,7 +5,7 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
public class OnEntityDamageEventHandler {
public class OnPlayerDamageEventHandler {
@SubscribeEvent
public void LivingDamageEvent(LivingDamageEvent event) {
if (!(event.getEntity() instanceof PlayerEntity)) { return; }

View File

@ -7,7 +7,7 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class OnEntityJoinEventHandler {
public class OnPlayerJoinEventHandler {
@SubscribeEvent
public void EntityJoinWorldEvent(EntityJoinWorldEvent event) {
if (!(event.getEntity() instanceof PlayerEntity)) { return; }

View 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 OnServerTickEventHandler {
@SubscribeEvent
public void ServerTickEvent(TickEvent.ServerTickEvent event) {
if (LoginProtection.protected_players.size() == 0) { return; }
LoginProtection.protected_players.updateGracePeriods();
}
}

View File

@ -0,0 +1,29 @@
package com.micle.loginprotection.network;
import com.micle.loginprotection.LoginProtection;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier;
public class C2SKeyPress {
public C2SKeyPress() { }
public static void encode(final C2SKeyPress msg, final PacketBuffer packet_buffer) { }
public static C2SKeyPress decode(final PacketBuffer 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 ServerPlayerEntity sender = context.getSender();
if (sender == null) { return; }
LoginProtection.protected_players.getPlayer(sender.getUUID()).setLoading(false);
});
context.setPacketHandled(true);
}
}

View File

@ -1,16 +1,37 @@
package com.micle.loginprotection.setup;
import com.micle.loginprotection.events.OnEntityDamageEventHandler;
import com.micle.loginprotection.events.OnEntityJoinEventHandler;
import com.micle.loginprotection.LoginProtection;
import com.micle.loginprotection.events.OnPlayerDamageEventHandler;
import com.micle.loginprotection.events.OnPlayerJoinEventHandler;
import com.micle.loginprotection.network.C2SKeyPress;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;
public class Registration {
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 static void register() {
IEventBus mod_event_bus = FMLJavaModLoadingContext.get().getModEventBus();
MinecraftForge.EVENT_BUS.register(new OnEntityJoinEventHandler());
MinecraftForge.EVENT_BUS.register(new OnEntityDamageEventHandler());
MinecraftForge.EVENT_BUS.register(new OnPlayerJoinEventHandler());
MinecraftForge.EVENT_BUS.register(new OnPlayerDamageEventHandler());
int id = 0;
INSTANCE.registerMessage(id++,
C2SKeyPress.class,
C2SKeyPress::encode,
C2SKeyPress::decode,
C2SKeyPress::handle
);
}
}