Finishing Forge touches

This commit is contained in:
samolego 2021-01-26 14:18:05 +01:00
parent 6c8d80ac20
commit 37d4f207e2
2 changed files with 75 additions and 4 deletions

View File

@ -1,22 +1,18 @@
package org.samo_lego.simpleauth;
import com.mojang.brigadier.CommandDispatcher;
import me.shedaniel.architectury.platform.forge.EventBuses;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.server.FMLServerStoppedEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLPaths;
import org.samo_lego.simpleauth.commands.*;
@Mod(SimpleAuth.MOD_ID)
public class SimpleAuthForge {
public SimpleAuthForge() {
// Submit our event bus to let architectury register our content on the right time
//EventBuses.registerModEventBus(SimpleAuth.MOD_ID, FMLJavaModLoadingContext.get().getModEventBus());
SimpleAuth.init(FMLPaths.GAMEDIR.get());
MinecraftForge.EVENT_BUS.register(this);
}

View File

@ -0,0 +1,75 @@
package org.samo_lego.simpleauth.event;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.TypedActionResult;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.samo_lego.simpleauth.utils.PlayerAuth;
import static net.minecraftforge.eventbus.api.EventPriority.HIGHEST;
import static org.samo_lego.simpleauth.SimpleAuth.MOD_ID;
import static org.samo_lego.simpleauth.SimpleAuth.config;
/**
* This class will take care of actions players try to do,
* and cancel them if they aren't authenticated
*/
@Mod.EventBusSubscriber(modid = MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class AuthEventHandlerForge {
@SubscribeEvent(priority = HIGHEST)
public static void onContainerOpen(PlayerContainerEvent.Open event) {
PlayerEntity player = event.getPlayer();
if(AuthEventHandler.onUseBlock(player).equals(ActionResult.FAIL)) {
event.setCanceled(true);
}
}
// Using a block (right-click function)
@SubscribeEvent(priority = HIGHEST)
public static void onUseBlock(PlayerInteractEvent.RightClickBlock event) {
PlayerEntity player = event.getPlayer();
if(AuthEventHandler.onUseBlock(player).equals(ActionResult.FAIL)) {
event.setCanceled(true);
}
}
// Punching a block
@SubscribeEvent(priority = HIGHEST)
public static void onAttackBlock(PlayerInteractEvent.LeftClickBlock event) {
PlayerEntity player = event.getPlayer();
if(AuthEventHandler.onBreakBlock(player)) {
event.setCanceled(true);
}
}
// Using an item
@SubscribeEvent(priority = HIGHEST)
public static void onUseItem(PlayerInteractEvent.RightClickItem event) {
PlayerEntity player = event.getPlayer();
if(AuthEventHandler.onUseItem(player).getResult().equals(ActionResult.FAIL)) {
event.setCanceled(true);
}
}
// Attacking an entity
@SubscribeEvent(priority = HIGHEST)
public static void onAttackEntity(AttackEntityEvent event) {
PlayerEntity player = event.getPlayer();
if(AuthEventHandler.onAttackEntity(player).equals(ActionResult.FAIL)) {
event.setCanceled(true);
}
}
// Interacting with entity
@SubscribeEvent(priority = HIGHEST)
public static void onUseEntity(PlayerInteractEvent.EntityInteract event) {
PlayerEntity player = event.getPlayer();
if(AuthEventHandler.onUseEntity(player).equals(ActionResult.FAIL)) {
event.setCanceled(true);
}
}
}