Merge remote-tracking branch 'origin/1.16-experimental'
# Conflicts: # src/main/java/com/flemmli97/flan/api/PermissionRegistry.java # src/main/java/com/flemmli97/flan/event/EntityInteractEvents.java
This commit is contained in:
commit
2f0913c2bc
@ -68,6 +68,7 @@ public class PermissionRegistry {
|
||||
public static ClaimPermission PICKUP = register(new ClaimPermission("PICKUP", () -> new ItemStack(Items.BRICK), true, "Allow the pickup of items"));
|
||||
public static ClaimPermission FLIGHT = register(new ClaimPermission("FLIGHT", () -> new ItemStack(Items.FEATHER), true, "Allow non creative flight"));
|
||||
|
||||
public static ClaimPermission CANSTAY = register(new ClaimPermission("CANSTAY", () -> new ItemStack(Items.PAPER), true, "Allow player to enter your claim"));
|
||||
public static ClaimPermission HURTPLAYER = global(new ClaimPermission("HURTPLAYER", () -> new ItemStack(Items.DIAMOND_SWORD), "Permission to hurt other players"));
|
||||
public static ClaimPermission EXPLOSIONS = global(new ClaimPermission("EXPLOSIONS", () -> new ItemStack(Items.TNT), "Toggle explosions in claim"));
|
||||
public static ClaimPermission WITHER = global(new ClaimPermission("WITHER", () -> new ItemStack(Items.WITHER_SKELETON_SKULL), "Toggle wither breaking blocks in claim"));
|
||||
@ -101,7 +102,7 @@ public class PermissionRegistry {
|
||||
|
||||
public static ClaimPermission get(String id) {
|
||||
if (!permissions.containsKey(id))
|
||||
throw new NullPointerException("No such permission " + id + " registered");
|
||||
throw new NullPointerException("No such permission registered");
|
||||
return permissions.get(id);
|
||||
}
|
||||
|
||||
|
38
src/main/java/com/flemmli97/flan/claim/Benchmark.java
Normal file
38
src/main/java/com/flemmli97/flan/claim/Benchmark.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.flemmli97.flan.claim;
|
||||
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Benchmark {
|
||||
|
||||
private final StopWatch stopWatch = new StopWatch();
|
||||
private final List<Long> times = new LinkedList<>();
|
||||
|
||||
public Benchmark() {
|
||||
}
|
||||
public void start() {
|
||||
stopWatch.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
stopWatch.stop();
|
||||
times.add(stopWatch.getNanoTime());
|
||||
stopWatch.reset();
|
||||
}
|
||||
|
||||
public void reset(){
|
||||
times.clear();
|
||||
stopWatch.reset();
|
||||
}
|
||||
|
||||
public long getAverage() {
|
||||
return times.stream().reduce(0L, Long::sum)/times.size();
|
||||
}
|
||||
|
||||
public long worstCase() {
|
||||
return times.stream().max(Long::compareTo).orElse(-1L);
|
||||
}
|
||||
}
|
@ -2,11 +2,14 @@ package com.flemmli97.flan.event;
|
||||
|
||||
import com.flemmli97.flan.api.ClaimPermission;
|
||||
import com.flemmli97.flan.api.PermissionRegistry;
|
||||
import com.flemmli97.flan.claim.Benchmark;
|
||||
import com.flemmli97.flan.claim.Claim;
|
||||
import com.flemmli97.flan.claim.ClaimStorage;
|
||||
import com.flemmli97.flan.claim.IPermissionContainer;
|
||||
import com.flemmli97.flan.claim.ObjectToPermissionMap;
|
||||
import com.flemmli97.flan.mixin.IPersistentProjectileVars;
|
||||
import com.flemmli97.flan.player.IOwnedItem;
|
||||
import com.flemmli97.flan.player.IPlayerClaimImpl;
|
||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.entity.Entity;
|
||||
@ -37,14 +40,19 @@ import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.Hand;
|
||||
import net.minecraft.util.Pair;
|
||||
import net.minecraft.util.collection.DefaultedList;
|
||||
import net.minecraft.util.hit.BlockHitResult;
|
||||
import net.minecraft.util.hit.EntityHitResult;
|
||||
import net.minecraft.util.hit.HitResult;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.math.Direction;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.world.World;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class EntityInteractEvents {
|
||||
|
||||
@ -278,4 +286,75 @@ public class EntityInteractEvents {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static final Benchmark benchmark = new Benchmark();
|
||||
|
||||
public static void updateClaim(ServerPlayerEntity player, Consumer<Claim> cons) {
|
||||
benchmark.start();
|
||||
if(player.age % 5 == 0) {
|
||||
ClaimStorage storage = ClaimStorage.get(player.getServerWorld());
|
||||
Vec3d pos = player.getPos();
|
||||
BlockPos.Mutable bPos = new BlockPos(pos).mutableCopy();
|
||||
Claim claim = storage.getClaimAt(bPos);
|
||||
cons.accept(claim);
|
||||
if(!player.isSpectator()) {
|
||||
if (claim != null && !claim.canInteract(player, PermissionRegistry.CANSTAY, bPos, true)) {
|
||||
Vec3d tp = getTeleportPos(player, pos, storage, claim.getDimensions(), bPos);
|
||||
player.teleport(tp.getX(), tp.getY(), tp.getZ());
|
||||
}
|
||||
}
|
||||
}
|
||||
benchmark.stop();
|
||||
System.out.println(String.format("Average: %d, Worst: %d", benchmark.getAverage(), benchmark.worstCase()));
|
||||
}
|
||||
|
||||
private static Vec3d getTeleportPos(ServerPlayerEntity player, Vec3d playerPos, ClaimStorage storage, int [] dim, BlockPos.Mutable bPos) {
|
||||
Pair<Direction, Vec3d> pos = nearestOutside(dim, playerPos);
|
||||
bPos.set(pos.getRight().getX(), pos.getRight().getY(), pos.getRight().getZ());
|
||||
Claim claim = storage.getClaimAt(bPos);
|
||||
if(claim == null || claim.canInteract(player, PermissionRegistry.CANSTAY, bPos, false))
|
||||
return pos.getRight();
|
||||
int[] newDim = claim.getDimensions();
|
||||
switch (pos.getLeft()){
|
||||
case NORTH:
|
||||
dim[2] = newDim[2];
|
||||
break;
|
||||
case SOUTH:
|
||||
dim[3] = newDim[3];
|
||||
break;
|
||||
case EAST:
|
||||
dim[1] = newDim[1];
|
||||
break;
|
||||
default:
|
||||
dim[0] = newDim[0];
|
||||
break;
|
||||
}
|
||||
return getTeleportPos(player, playerPos, storage, dim, bPos);
|
||||
}
|
||||
|
||||
|
||||
private static Pair<Direction, Vec3d> nearestOutside(int[] dim, Vec3d from) {
|
||||
double northDist = Math.abs(from.getZ() - dim[2]);
|
||||
double southDist = Math.abs(dim[3] - from.getZ());
|
||||
double westDist = Math.abs(from.getX() - dim[0]);
|
||||
double eastDist = Math.abs(dim[1] - from.getX());
|
||||
if(northDist > southDist) {
|
||||
if(eastDist > westDist) {
|
||||
if(southDist > westDist)
|
||||
return new Pair<>(Direction.WEST, new Vec3d(dim[0] - 1.5, from.getY(), from.getZ()));
|
||||
return new Pair<>(Direction.SOUTH, new Vec3d(from.getX(), from.getY(), dim[3] + 1.5));
|
||||
}
|
||||
if(southDist > eastDist)
|
||||
return new Pair<>(Direction.EAST, new Vec3d(dim[1] + 1.5, from.getY(), from.getZ()));
|
||||
return new Pair<>(Direction.SOUTH, new Vec3d(from.getX(), from.getY(), dim[3]+1.5));
|
||||
}
|
||||
if(eastDist > westDist) {
|
||||
if(northDist > westDist)
|
||||
return new Pair<>(Direction.WEST, new Vec3d(dim[0] - 1.5, from.getY(), from.getZ()));
|
||||
return new Pair<>(Direction.NORTH, new Vec3d(from.getX(), from.getY(), dim[2] - 1.5));
|
||||
}
|
||||
if(northDist > eastDist)
|
||||
return new Pair<>(Direction.EAST, new Vec3d(dim[1] + 1.5, from.getY(), from.getZ()));
|
||||
return new Pair<>(Direction.NORTH, new Vec3d(from.getX(), from.getY(), dim[2]-1.5));
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
package com.flemmli97.flan.mixin;
|
||||
|
||||
import com.flemmli97.flan.claim.Claim;
|
||||
import com.flemmli97.flan.claim.ClaimStorage;
|
||||
import com.flemmli97.flan.event.EntityInteractEvents;
|
||||
import com.flemmli97.flan.player.IPlayerClaimImpl;
|
||||
import com.flemmli97.flan.player.PlayerClaimData;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
@ -18,6 +21,9 @@ public abstract class PlayerClaimMixin implements IPlayerClaimImpl {
|
||||
@Unique
|
||||
private PlayerClaimData claimData;
|
||||
|
||||
@Unique
|
||||
private Claim currentClaim;
|
||||
|
||||
@Shadow
|
||||
private MinecraftServer server;
|
||||
|
||||
@ -46,8 +52,18 @@ public abstract class PlayerClaimMixin implements IPlayerClaimImpl {
|
||||
this.claimData.clone(PlayerClaimData.get(oldPlayer));
|
||||
}
|
||||
|
||||
@Inject(method = "tick", at = @At("HEAD"))
|
||||
private void canStay(CallbackInfo info) {
|
||||
EntityInteractEvents.updateClaim((ServerPlayerEntity)(Object)this, claim->this.currentClaim = claim);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerClaimData get() {
|
||||
return this.claimData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Claim getCurrentClaim() {
|
||||
return this.currentClaim;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,13 @@
|
||||
package com.flemmli97.flan.player;
|
||||
|
||||
import com.flemmli97.flan.claim.Claim;
|
||||
|
||||
public interface IPlayerClaimImpl {
|
||||
|
||||
PlayerClaimData get();
|
||||
|
||||
/**
|
||||
* @return Gets the current claim the player is in. Can be null if not in a claim.
|
||||
*/
|
||||
Claim getCurrentClaim();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user