2020-08-23 12:52:36 +00:00
|
|
|
package com.flemmli97.flan.event;
|
|
|
|
|
2021-04-11 21:34:45 +00:00
|
|
|
import com.flemmli97.flan.api.ClaimPermission;
|
2020-11-28 19:53:29 +00:00
|
|
|
import com.flemmli97.flan.api.PermissionRegistry;
|
2020-08-23 12:52:36 +00:00
|
|
|
import com.flemmli97.flan.claim.Claim;
|
|
|
|
import com.flemmli97.flan.claim.ClaimStorage;
|
2020-10-30 14:09:57 +00:00
|
|
|
import com.flemmli97.flan.claim.IPermissionContainer;
|
2021-04-11 21:34:45 +00:00
|
|
|
import com.flemmli97.flan.claim.ObjectToPermissionMap;
|
2020-08-24 19:03:06 +00:00
|
|
|
import com.flemmli97.flan.claim.PermHelper;
|
2020-08-23 12:52:36 +00:00
|
|
|
import com.flemmli97.flan.config.ConfigHandler;
|
2021-06-08 19:01:38 +00:00
|
|
|
import com.flemmli97.flan.integration.permissionapi.CommandPermission;
|
2020-08-23 21:18:18 +00:00
|
|
|
import com.flemmli97.flan.player.EnumDisplayType;
|
2020-08-23 12:52:36 +00:00
|
|
|
import com.flemmli97.flan.player.EnumEditMode;
|
|
|
|
import com.flemmli97.flan.player.PlayerClaimData;
|
2021-05-02 16:24:46 +00:00
|
|
|
import com.google.common.collect.Sets;
|
2020-08-23 12:52:36 +00:00
|
|
|
import com.mojang.authlib.GameProfile;
|
2020-08-24 23:20:22 +00:00
|
|
|
import net.minecraft.block.BlockState;
|
2020-08-23 12:52:36 +00:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2020-08-26 14:39:44 +00:00
|
|
|
import net.minecraft.item.BlockItem;
|
2020-08-24 23:20:22 +00:00
|
|
|
import net.minecraft.item.Item;
|
2020-08-26 23:34:39 +00:00
|
|
|
import net.minecraft.item.ItemPlacementContext;
|
2020-08-23 12:52:36 +00:00
|
|
|
import net.minecraft.item.ItemStack;
|
2020-08-24 23:20:22 +00:00
|
|
|
import net.minecraft.item.ItemUsageContext;
|
2020-08-23 12:52:36 +00:00
|
|
|
import net.minecraft.item.Items;
|
2020-08-26 18:15:05 +00:00
|
|
|
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
|
2021-04-11 21:34:45 +00:00
|
|
|
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
|
2020-08-23 21:18:18 +00:00
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
2020-08-23 12:52:36 +00:00
|
|
|
import net.minecraft.server.world.ServerWorld;
|
|
|
|
import net.minecraft.text.Text;
|
2020-08-24 23:20:22 +00:00
|
|
|
import net.minecraft.util.ActionResult;
|
2020-08-24 19:03:06 +00:00
|
|
|
import net.minecraft.util.Formatting;
|
2020-08-23 12:52:36 +00:00
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.TypedActionResult;
|
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
|
|
import net.minecraft.util.hit.HitResult;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2020-08-24 20:32:29 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
2020-08-23 12:52:36 +00:00
|
|
|
public class ItemInteractEvents {
|
|
|
|
|
2020-08-23 21:18:18 +00:00
|
|
|
public static TypedActionResult<ItemStack> useItem(PlayerEntity p, World world, Hand hand) {
|
2020-08-24 23:20:22 +00:00
|
|
|
if (world.isClient || p.isSpectator())
|
2020-08-23 21:18:18 +00:00
|
|
|
return TypedActionResult.pass(p.getStackInHand(hand));
|
|
|
|
ServerPlayerEntity player = (ServerPlayerEntity) p;
|
2020-08-23 12:52:36 +00:00
|
|
|
ItemStack stack = player.getStackInHand(hand);
|
|
|
|
if (stack.getItem() == ConfigHandler.config.claimingItem) {
|
|
|
|
HitResult ray = player.rayTrace(64, 0, false);
|
|
|
|
if (ray != null && ray.getType() == HitResult.Type.BLOCK) {
|
2020-08-24 19:03:06 +00:00
|
|
|
claimLandHandling(player, ((BlockHitResult) ray).getBlockPos());
|
|
|
|
return TypedActionResult.success(stack);
|
2020-08-23 12:52:36 +00:00
|
|
|
}
|
2020-08-24 19:03:06 +00:00
|
|
|
return TypedActionResult.pass(stack);
|
2020-08-23 12:52:36 +00:00
|
|
|
}
|
|
|
|
if (stack.getItem() == ConfigHandler.config.inspectionItem) {
|
|
|
|
HitResult ray = player.rayTrace(32, 0, false);
|
|
|
|
if (ray != null && ray.getType() == HitResult.Type.BLOCK) {
|
2020-08-24 19:03:06 +00:00
|
|
|
inspect(player, ((BlockHitResult) ray).getBlockPos());
|
|
|
|
return TypedActionResult.success(stack);
|
2020-08-23 12:52:36 +00:00
|
|
|
}
|
2020-08-24 19:03:06 +00:00
|
|
|
return TypedActionResult.pass(stack);
|
2020-08-23 12:52:36 +00:00
|
|
|
}
|
2021-04-11 21:34:45 +00:00
|
|
|
|
2020-08-23 12:52:36 +00:00
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) world);
|
|
|
|
BlockPos pos = player.getBlockPos();
|
2020-10-30 14:09:57 +00:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(pos);
|
2020-08-23 12:52:36 +00:00
|
|
|
if (claim == null)
|
|
|
|
return TypedActionResult.pass(stack);
|
2021-04-11 21:34:45 +00:00
|
|
|
ClaimPermission perm = ObjectToPermissionMap.getFromItem(stack.getItem());
|
|
|
|
if (perm != null)
|
|
|
|
return claim.canInteract(player, perm, pos, true) ? TypedActionResult.pass(stack) : TypedActionResult.fail(stack);
|
2020-08-23 12:52:36 +00:00
|
|
|
return TypedActionResult.pass(stack);
|
|
|
|
}
|
2020-08-24 19:03:06 +00:00
|
|
|
|
2021-05-02 16:24:46 +00:00
|
|
|
private static final Set<Item> blackListedItems = Sets.newHashSet(Items.COMPASS, Items.FILLED_MAP, Items.FIREWORK_ROCKET);
|
2020-08-25 17:43:52 +00:00
|
|
|
|
|
|
|
public static ActionResult onItemUseBlock(ItemUsageContext context) {
|
2020-11-07 12:44:26 +00:00
|
|
|
//Check for Fakeplayer. Since there is no api for that directly check the class
|
|
|
|
if (!(context.getPlayer() instanceof ServerPlayerEntity) || !context.getPlayer().getClass().equals(ServerPlayerEntity.class) || context.getStack().isEmpty())
|
2020-08-24 23:20:22 +00:00
|
|
|
return ActionResult.PASS;
|
|
|
|
ClaimStorage storage = ClaimStorage.get((ServerWorld) context.getWorld());
|
2020-08-26 23:34:39 +00:00
|
|
|
BlockPos placePos = new ItemPlacementContext(context).getBlockPos();
|
2020-10-30 14:09:57 +00:00
|
|
|
IPermissionContainer claim = storage.getForPermissionCheck(placePos.add(0, 255, 0));
|
2020-08-25 17:43:52 +00:00
|
|
|
if (claim == null)
|
2020-08-24 23:20:22 +00:00
|
|
|
return ActionResult.PASS;
|
2020-08-25 17:43:52 +00:00
|
|
|
if (blackListedItems.contains(context.getStack().getItem()))
|
2020-08-24 23:20:22 +00:00
|
|
|
return ActionResult.PASS;
|
2020-10-30 14:09:57 +00:00
|
|
|
boolean actualInClaim = !(claim instanceof Claim) || placePos.getY() >= ((Claim) claim).getDimensions()[4];
|
2020-08-24 23:20:22 +00:00
|
|
|
ServerPlayerEntity player = (ServerPlayerEntity) context.getPlayer();
|
2021-04-11 21:34:45 +00:00
|
|
|
ClaimPermission perm = ObjectToPermissionMap.getFromItem(context.getStack().getItem());
|
|
|
|
if (perm != null) {
|
|
|
|
if (claim.canInteract(player, perm, placePos, false))
|
2020-08-24 23:20:22 +00:00
|
|
|
return ActionResult.PASS;
|
2020-09-02 13:36:58 +00:00
|
|
|
else if (actualInClaim) {
|
2020-08-26 18:15:05 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
|
|
|
|
return ActionResult.FAIL;
|
|
|
|
}
|
2020-08-24 23:20:22 +00:00
|
|
|
}
|
2020-11-28 19:53:29 +00:00
|
|
|
if (claim.canInteract(player, PermissionRegistry.PLACE, placePos, false)) {
|
2020-09-02 13:36:58 +00:00
|
|
|
if (!actualInClaim && context.getStack().getItem() instanceof BlockItem) {
|
2020-10-30 14:09:57 +00:00
|
|
|
((Claim) claim).extendDownwards(placePos);
|
2020-08-26 14:39:44 +00:00
|
|
|
}
|
2020-08-24 23:20:22 +00:00
|
|
|
return ActionResult.PASS;
|
2020-09-02 13:36:58 +00:00
|
|
|
} else if (actualInClaim) {
|
2020-08-26 18:15:05 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
|
|
|
|
BlockState other = context.getWorld().getBlockState(placePos.up());
|
|
|
|
player.networkHandler.sendPacket(new BlockUpdateS2CPacket(placePos.up(), other));
|
|
|
|
PlayerClaimData.get(player).addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
|
2021-04-11 21:34:45 +00:00
|
|
|
updateHeldItem(player);
|
2020-08-26 18:15:05 +00:00
|
|
|
return ActionResult.FAIL;
|
|
|
|
}
|
|
|
|
return ActionResult.PASS;
|
2020-08-24 23:20:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:34:45 +00:00
|
|
|
/**
|
|
|
|
* -2 == Main inventory update
|
|
|
|
*/
|
|
|
|
private static void updateHeldItem(ServerPlayerEntity player) {
|
|
|
|
player.networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(-2, player.inventory.selectedSlot, player.inventory.getMainHandStack()));
|
|
|
|
player.networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(-2, 40, player.inventory.getStack(40)));
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:43:52 +00:00
|
|
|
private static boolean cantClaimInWorld(ServerWorld world) {
|
|
|
|
for (String s : ConfigHandler.config.blacklistedWorlds) {
|
|
|
|
if (s.equals(world.getRegistryKey().getValue().toString())) {
|
2020-08-24 19:46:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:43:52 +00:00
|
|
|
public static void claimLandHandling(ServerPlayerEntity player, BlockPos target) {
|
2021-03-09 12:29:26 +00:00
|
|
|
if (!CommandPermission.perm(player, CommandPermission.claimCreate, false)) {
|
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermission, Formatting.DARK_RED), true);
|
|
|
|
return;
|
|
|
|
}
|
2020-08-25 17:43:52 +00:00
|
|
|
if (ConfigHandler.config.worldWhitelist) {
|
|
|
|
if (!cantClaimInWorld(player.getServerWorld())) {
|
2020-08-24 19:03:06 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.landClaimDisabledWorld, Formatting.DARK_RED), false);
|
|
|
|
return;
|
|
|
|
}
|
2020-08-25 17:43:52 +00:00
|
|
|
} else if (cantClaimInWorld(player.getServerWorld())) {
|
2020-08-24 19:46:25 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.landClaimDisabledWorld, Formatting.DARK_RED), false);
|
|
|
|
return;
|
|
|
|
}
|
2020-08-24 19:03:06 +00:00
|
|
|
ClaimStorage storage = ClaimStorage.get(player.getServerWorld());
|
2020-09-02 13:36:58 +00:00
|
|
|
Claim claim = storage.getClaimAt(target.add(0, 255, 0));
|
2020-08-24 19:03:06 +00:00
|
|
|
PlayerClaimData data = PlayerClaimData.get(player);
|
2020-09-02 13:36:58 +00:00
|
|
|
if (data.claimCooldown())
|
2020-08-26 18:15:05 +00:00
|
|
|
return;
|
|
|
|
data.setClaimActionCooldown();
|
2020-08-24 19:03:06 +00:00
|
|
|
if (claim != null) {
|
2020-11-28 19:53:29 +00:00
|
|
|
if (claim.canInteract(player, PermissionRegistry.EDITCLAIM, target)) {
|
2020-08-24 19:03:06 +00:00
|
|
|
if (data.getEditMode() == EnumEditMode.SUBCLAIM) {
|
|
|
|
Claim subClaim = claim.getSubClaim(target);
|
2020-08-25 17:43:52 +00:00
|
|
|
if (subClaim != null && data.currentEdit() == null) {
|
2020-08-24 19:03:06 +00:00
|
|
|
if (subClaim.isCorner(target)) {
|
2020-08-24 20:19:09 +00:00
|
|
|
data.setEditClaim(subClaim, player.getBlockPos().getY());
|
2020-08-24 19:03:06 +00:00
|
|
|
data.setEditingCorner(target);
|
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.resizeClaim, Formatting.GOLD), false);
|
2020-08-25 17:43:52 +00:00
|
|
|
} else {
|
2020-08-24 19:03:06 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.cantClaimHere, Formatting.RED), false);
|
|
|
|
}
|
2020-08-25 19:50:41 +00:00
|
|
|
data.addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
|
2020-08-24 19:03:06 +00:00
|
|
|
} else {
|
2020-08-25 17:43:52 +00:00
|
|
|
if (data.currentEdit() != null) {
|
2020-09-02 13:36:58 +00:00
|
|
|
if (!data.editingCorner().equals(target)) {
|
2020-08-25 19:50:41 +00:00
|
|
|
Set<Claim> fl = claim.resizeSubclaim(data.currentEdit(), data.editingCorner(), target);
|
|
|
|
if (!fl.isEmpty()) {
|
|
|
|
fl.forEach(confl -> data.addDisplayClaim(confl, EnumDisplayType.MAIN, player.getBlockPos().getY()));
|
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.conflictOther, Formatting.RED), false);
|
|
|
|
}
|
|
|
|
data.setEditClaim(null, 0);
|
|
|
|
data.setEditingCorner(null);
|
2020-08-24 20:32:29 +00:00
|
|
|
}
|
2020-08-25 17:43:52 +00:00
|
|
|
} else if (data.editingCorner() != null) {
|
2020-09-02 13:36:58 +00:00
|
|
|
if (!data.editingCorner().equals(target)) {
|
2020-08-25 19:50:41 +00:00
|
|
|
Set<Claim> fl = claim.tryCreateSubClaim(data.editingCorner(), target);
|
|
|
|
data.addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
|
|
|
|
if (!fl.isEmpty()) {
|
2020-09-15 11:54:00 +00:00
|
|
|
fl.forEach(confl -> data.addDisplayClaim(confl, EnumDisplayType.CONFLICT, player.getBlockPos().getY()));
|
2020-08-25 19:50:41 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.conflictOther, Formatting.RED), false);
|
|
|
|
} else {
|
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.subClaimCreateSuccess, Formatting.GOLD), false);
|
|
|
|
}
|
|
|
|
data.setEditingCorner(null);
|
2020-08-24 19:03:06 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
data.setEditingCorner(target);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (claim.isCorner(target)) {
|
2020-08-24 20:19:09 +00:00
|
|
|
data.setEditClaim(claim, player.getBlockPos().getY());
|
2020-08-24 19:03:06 +00:00
|
|
|
data.setEditingCorner(target);
|
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.resizeClaim, Formatting.GOLD), false);
|
2020-08-25 17:43:52 +00:00
|
|
|
} else if (data.currentEdit() != null) {
|
2020-08-24 19:03:06 +00:00
|
|
|
storage.resizeClaim(data.currentEdit(), data.editingCorner(), target, player);
|
2020-08-24 20:19:09 +00:00
|
|
|
data.setEditClaim(null, 0);
|
2020-08-24 19:03:06 +00:00
|
|
|
data.setEditingCorner(null);
|
2020-08-25 17:43:52 +00:00
|
|
|
} else {
|
2020-08-24 20:19:09 +00:00
|
|
|
data.addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
|
2020-08-24 19:03:06 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.cantClaimHere, Formatting.RED), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-24 20:19:09 +00:00
|
|
|
data.addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
|
2020-08-24 19:03:06 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.cantClaimHere, Formatting.RED), false);
|
|
|
|
}
|
2020-08-25 17:43:52 +00:00
|
|
|
} else if (data.getEditMode() == EnumEditMode.SUBCLAIM) {
|
2020-08-24 19:03:06 +00:00
|
|
|
player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.wrongMode, data.getEditMode()), Formatting.RED), false);
|
2020-08-25 17:43:52 +00:00
|
|
|
} else {
|
|
|
|
if (data.currentEdit() != null) {
|
2020-08-24 19:03:06 +00:00
|
|
|
storage.resizeClaim(data.currentEdit(), data.editingCorner(), target, player);
|
2020-08-24 20:19:09 +00:00
|
|
|
data.setEditClaim(null, 0);
|
2020-08-24 19:03:06 +00:00
|
|
|
data.setEditingCorner(null);
|
2020-08-25 17:43:52 +00:00
|
|
|
} else if (data.editingCorner() != null) {
|
2020-08-24 19:03:06 +00:00
|
|
|
storage.createClaim(data.editingCorner(), target, player);
|
|
|
|
data.setEditingCorner(null);
|
2020-08-25 17:43:52 +00:00
|
|
|
} else
|
2020-08-24 19:03:06 +00:00
|
|
|
data.setEditingCorner(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 17:43:52 +00:00
|
|
|
public static void inspect(ServerPlayerEntity player, BlockPos target) {
|
2020-08-24 19:03:06 +00:00
|
|
|
Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(target);
|
2020-12-11 15:40:13 +00:00
|
|
|
PlayerClaimData data = PlayerClaimData.get(player);
|
|
|
|
if (data.claimCooldown())
|
|
|
|
return;
|
|
|
|
data.setClaimActionCooldown();
|
2020-08-24 19:03:06 +00:00
|
|
|
if (claim != null) {
|
2020-08-26 23:34:39 +00:00
|
|
|
String owner = claim.isAdminClaim() ? "<Admin>" : "<UNKOWN>";
|
|
|
|
if (!claim.isAdminClaim()) {
|
2020-08-24 19:03:06 +00:00
|
|
|
GameProfile prof = player.getServer().getUserCache().getByUuid(claim.getOwner());
|
|
|
|
if (prof != null && prof.getName() != null)
|
|
|
|
owner = prof.getName();
|
|
|
|
}
|
|
|
|
Text text = PermHelper.simpleColoredText(String.format(ConfigHandler.lang.inspectBlockOwner,
|
|
|
|
owner,
|
|
|
|
target.getX(), target.getY(), target.getZ()), Formatting.GREEN);
|
|
|
|
player.sendMessage(text, false);
|
2020-12-11 15:40:13 +00:00
|
|
|
data.addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
|
2020-08-24 19:03:06 +00:00
|
|
|
} else
|
|
|
|
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.inspectNoClaim, Formatting.RED), false);
|
|
|
|
}
|
2020-08-23 12:52:36 +00:00
|
|
|
}
|