Changed grace period time from 200 to 400 (5s to 10s). Added function to ProtectedPlayers.java to remove a player from the list, updated the updateGracePeriods method. Updated C2SKeyPress.java. Made OnKeyPressEventHandler.java check for mouse clicks as well, also made sure it only fires on client-side. Added an event for when a player leaves the server to remove them from the list.
This commit is contained in:
@ -3,8 +3,11 @@ package com.micle.loginprotection;
|
||||
import com.micle.loginprotection.data.ProtectedPlayer;
|
||||
import com.micle.loginprotection.data.ProtectedPlayers;
|
||||
import com.micle.loginprotection.setup.Registration;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.network.simple.SimpleChannel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -15,6 +18,14 @@ public class 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();
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ public class ProtectedPlayer {
|
||||
|
||||
public ProtectedPlayer(UUID player_uuid) {
|
||||
this.player_uuid = player_uuid;
|
||||
this.grace_period = 200;
|
||||
this.grace_period = 400;
|
||||
this.is_loading = true;
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,13 @@
|
||||
package com.micle.loginprotection.data;
|
||||
|
||||
import net.minecraft.entity.ai.attributes.Attribute;
|
||||
import net.minecraft.entity.ai.attributes.Attributes;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.potion.EffectInstance;
|
||||
import net.minecraft.potion.Effects;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraftforge.fml.server.ServerLifecycleHooks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@ -29,18 +37,25 @@ public class ProtectedPlayers {
|
||||
}
|
||||
|
||||
public void removePlayer(UUID player_uuid) {
|
||||
ProtectedPlayer player = getPlayer(player_uuid);
|
||||
ProtectedPlayer protected_player = getPlayer(player_uuid);
|
||||
if (protected_player == null) { return; }
|
||||
protected_players.remove(protected_player);
|
||||
|
||||
ServerPlayerEntity player = ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayer(player_uuid);
|
||||
if (player == null) { return; }
|
||||
protected_players.remove(player);
|
||||
player.sendMessage(new StringTextComponent("Grace Period over!"), player_uuid);
|
||||
}
|
||||
|
||||
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 (protected_player.isLoading()) { continue; }
|
||||
|
||||
int grace_period = protected_player.getGracePeriod()-1;
|
||||
protected_player.setGracePeriod(grace_period);
|
||||
if (grace_period <= 0) {
|
||||
removePlayer(protected_player.getPlayerUUID());
|
||||
System.out.println(protected_player.getPlayerUUID() + " is no longer being protected!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +1,28 @@
|
||||
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) {
|
||||
if (LoginProtection.has_pressed_key) { return; }
|
||||
ServerData server = Minecraft.getInstance().getCurrentServer();
|
||||
if (server == null) { return; }
|
||||
LoginProtection.INSTANCE.sendToServer(new C2SKeyPress());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public void MouseClickEvent(InputEvent.ClickInputEvent event) {
|
||||
ServerData server = Minecraft.getInstance().getCurrentServer();
|
||||
if (server == null) { return; }
|
||||
LoginProtection.INSTANCE.sendToServer(new C2SKeyPress());
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,14 +5,16 @@ import com.micle.loginprotection.data.ProtectedPlayer;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
|
||||
public class OnPlayerJoinEventHandler {
|
||||
@SubscribeEvent
|
||||
public void EntityJoinWorldEvent(EntityJoinWorldEvent event) {
|
||||
public void EntityJoinWorldEvent(PlayerEvent.PlayerLoggedInEvent event) {
|
||||
if (!(event.getEntity() instanceof PlayerEntity)) { return; }
|
||||
PlayerEntity player = (PlayerEntity) 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.entity.player.PlayerEntity;
|
||||
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 PlayerEntity)) { return; }
|
||||
PlayerEntity player = event.getPlayer();
|
||||
|
||||
LoginProtection.protected_players.removePlayer(player.getUUID());
|
||||
}
|
||||
}
|
||||
@ -7,8 +7,6 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,7 +3,10 @@ package com.micle.loginprotection.network;
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraftforge.fml.network.NetworkDirection;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
import net.minecraftforge.fml.network.PacketDistributor;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ -21,8 +24,14 @@ public class C2SKeyPress {
|
||||
context.enqueueWork(() -> {
|
||||
final ServerPlayerEntity 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 StringTextComponent("Grace Period started!"), sender.getUUID());
|
||||
if (sender.isInWater()) {
|
||||
sender.setAirSupply(sender.getMaxAirSupply());
|
||||
}
|
||||
});
|
||||
context.setPacketHandled(true);
|
||||
}
|
||||
|
||||
@ -1,33 +1,24 @@
|
||||
package com.micle.loginprotection.setup;
|
||||
|
||||
import com.micle.loginprotection.LoginProtection;
|
||||
import com.micle.loginprotection.events.OnPlayerDamageEventHandler;
|
||||
import com.micle.loginprotection.events.OnPlayerJoinEventHandler;
|
||||
import com.micle.loginprotection.events.*;
|
||||
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 OnPlayerJoinEventHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new OnPlayerDamageEventHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new OnServerTickEventHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new OnKeyPressEventHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new OnPlayerLeaveEventHandler());
|
||||
|
||||
int id = 0;
|
||||
INSTANCE.registerMessage(id++,
|
||||
LoginProtection.INSTANCE.registerMessage(id++,
|
||||
C2SKeyPress.class,
|
||||
C2SKeyPress::encode,
|
||||
C2SKeyPress::decode,
|
||||
|
||||
Reference in New Issue
Block a user