First commit

This commit is contained in:
2021-09-26 22:47:34 +01:00
commit 3dde554e9c
16 changed files with 1100 additions and 0 deletions

View File

View 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);
}
}

View 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;
}
}

View 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!");
}
}
}

View 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) { }
}
}

View File

@ -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);
}
}
}

View 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!");
}
}

View File

@ -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());
}
}

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 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());
}
}

View 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);
}
}

View 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
);
}
}

View File

@ -0,0 +1,58 @@
# This is an example mods.toml file. It contains the data relating to the loading mods.
# There are several mandatory fields (#mandatory), and many more that are optional (#optional).
# The overall format is standard TOML format, v0.5.0.
# Note that there are a couple of TOML lists in this file.
# Find more information on toml format here: https://github.com/toml-lang/toml
# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml
modLoader="javafml" #mandatory
# A version range to match for said mod loader - for regular FML @Mod it will be the forge version
loaderVersion="[37,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions.
# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties.
# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here.
license="All rights reserved"
# A URL to refer people to when problems occur with this mod
#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional
# A list of mods - how many allowed here is determined by the individual mod loader
[[mods]] #mandatory
# The modid of the mod
modId="loginprotection" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
# ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata
# see the associated build.gradle script for how to populate this completely automatically during a build
version="${file.jarVersion}" #mandatory
# A display name for the mod
displayName="Micle's Login Protection" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/
#updateJSONURL="https://change.me.example.invalid/updates.json" #optional
# A URL for the "homepage" for this mod, displayed in the mod UI
#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional
# A file name (in the root of the mod JAR) containing a logo for display
#logoFile="examplemod.png" #optional
# A text field displayed in the mod UI
#credits="Thanks for this example mod goes to Java" #optional
# A text field displayed in the mod UI
authors="Micle" #optional
# The description text for the mod (multi line!) (#mandatory)
description='''
Mod for protecting players from damage while joining a server.
'''
# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional.
[[dependencies.loginprotection]] #optional
# the modid of the dependency
modId="forge" #mandatory
# Does this dependency have to exist - if not, ordering below must be specified
mandatory=true #mandatory
# The version range of the dependency
versionRange="[37,)" #mandatory
# An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory
ordering="NONE"
# Side this dependency is applied on - BOTH, CLIENT or SERVER
side="BOTH"
# Here's another dependency
[[dependencies.loginprotection]]
modId="minecraft"
mandatory=true
# This version range declares a minimum of the current minecraft version up to but not including the next major version
versionRange="[1.17.1,1.18)"
ordering="NONE"
side="BOTH"

7
src/main/resources/pack.mcmeta Executable file
View File

@ -0,0 +1,7 @@
{
"pack": {
"description": "loginprotection resources",
"pack_format": 6,
"_comment": "A pack_format of 6 requires json lang files and some texture changes from 1.16.2. Note: we require v6 pack meta for all mods."
}
}