Skip to content

Commit

Permalink
Portal gun tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jul 9, 2023
1 parent 69e7d39 commit 54d0904
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class PortalCubedGameRules {
public static final GameRules.Key<GameRules.IntegerValue> PORTAL_ALIGNMENT = GameRuleRegistry.register(
"portalAlignment", CATEGORY, GameRuleFactory.createIntRule(16, 0)
);
public static final GameRules.Key<GameRules.BooleanValue> DISABLE_PORTAL_VALIDATION = GameRuleRegistry.register(
"disablePortalValidation", CATEGORY, GameRuleFactory.createBooleanRule(false)
);
public static final GameRules.Key<GameRules.BooleanValue> USE_PORTAL_HUD = GameRuleRegistry.register(
"usePortalHud", CATEGORY, GameRuleFactory.createBooleanRule(false, (server, rule) -> {
final FriendlyByteBuf buf = PacketByteBufs.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.fusionflux.portalcubed.commands;

import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;

import com.fusionflux.portalcubed.entity.Portal;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import org.joml.Quaternionf;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.Entity;
import org.joml.Quaternionf;

import static net.minecraft.commands.Commands.argument;
import static net.minecraft.commands.Commands.literal;

public class RotatePortalCommand {
private static final SimpleCommandExceptionType NOT_PORTAL = new SimpleCommandExceptionType(Component.literal("Entity is not a Portal"));
Expand Down Expand Up @@ -44,7 +43,7 @@ private static int rotatePortal(CommandContext<CommandSourceStack> ctx) throws C
float y = FloatArgumentType.getFloat(ctx, "y");
float z = FloatArgumentType.getFloat(ctx, "z");
float w = FloatArgumentType.getFloat(ctx, "w");
portal.setValidation(false);
portal.setDisableValidation(true);
portal.getRotation().lerpTo(new Quaternionf(x, y, z, w).normalize());
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fusionflux/portalcubed/entity/Portal.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ public void onSyncedDataUpdated(EntityDataAccessor<?> key) {
}
}

public void setValidation(boolean validate) {
this.disableValidation = !validate;
public void setDisableValidation(boolean disableValidation) {
this.disableValidation = disableValidation;
}

public boolean validate() {
Expand Down
238 changes: 119 additions & 119 deletions src/main/java/com/fusionflux/portalcubed/items/PortalGun.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,164 +153,164 @@ protected boolean allowLinkingToOther() {
return false;
}

protected void shoot(Level world, Player user, InteractionHand hand, boolean leftClick) {
protected void shoot(Level level, Player user, InteractionHand hand, boolean leftClick) {
if (user.isSpectator() || PortalCubedComponents.HOLDER_COMPONENT.get(user).entityBeingHeld() != null) return;
ItemStack stack = user.getItemInHand(hand);
stack.getOrCreateTag().putBoolean("complementary", !leftClick);
if (!world.isClientSide) {
if (!level.isClientSide) {
level.playSound(null, user.position().x(), user.position().y(), user.position().z(), leftClick ? PortalCubedSounds.FIRE_EVENT_PRIMARY : PortalCubedSounds.FIRE_EVENT_SECONDARY, SoundSource.NEUTRAL, .3F, 1F);

Vec3i up;
Vec3i normal;
Vec3i right;
Vec3 blockPos;

HitResult hitResult = customRaycast(user, 128.0D, 0.0F);
if (hitResult.getType() != HitResult.Type.BLOCK) {
level.playSound(null, user.position().x(), user.position().y(), user.position().z(), PortalCubedSounds.INVALID_PORTAL_EVENT, SoundSource.NEUTRAL, 1F, 1F);
return;
}

CompoundTag tag = stack.getOrCreateTag();

Portal portalHolder;
Portal originalPortal;
CompoundTag portalsTag = tag.getCompound(world.dimension().location().toString());
CompoundTag portalsTag = tag.getCompound(level.dimension().location().toString());

if (portalsTag.contains((leftClick ? "Left" : "Right") + "Portal")) {
originalPortal = (Portal) ((ServerLevel) world).getEntity(portalsTag.getUUID((leftClick ? "Left" : "Right") + "Portal"));
originalPortal = (Portal) ((ServerLevel) level).getEntity(portalsTag.getUUID((leftClick ? "Left" : "Right") + "Portal"));
} else {
originalPortal = null;
}
portalHolder = PortalCubedEntities.PORTAL.create(world);
portalHolder = PortalCubedEntities.PORTAL.create(level);

Portal otherPortal;
if (portalsTag.contains((leftClick ? "Right" : "Left") + "Portal")) {
otherPortal = (Portal) ((ServerLevel) world).getEntity(portalsTag.getUUID((leftClick ? "Right" : "Left") + "Portal"));
otherPortal = (Portal) ((ServerLevel) level).getEntity(portalsTag.getUUID((leftClick ? "Right" : "Left") + "Portal"));
} else {
otherPortal = null;
}

world.playSound(null, user.position().x(), user.position().y(), user.position().z(), leftClick ? PortalCubedSounds.FIRE_EVENT_PRIMARY : PortalCubedSounds.FIRE_EVENT_SECONDARY, SoundSource.NEUTRAL, .3F, 1F);
normal = ((BlockHitResult) hitResult).getDirection().getOpposite().getNormal();
if (normal.getY() == 0) {
up = new Vec3i(0, 1, 0);
} else {
final Vec3 lookAngle = user.getLookAngle();
up = Direction.getNearest(lookAngle.x, 0, lookAngle.z).getNormal();
}
right = up.cross(normal);

Vec3i up;
Vec3i normal;
Vec3i right;
Vec3 blockPos;
final int alignment = level.getGameRules().getInt(PortalCubedGameRules.PORTAL_ALIGNMENT);
if (alignment == 0) {
blockPos = hitResult.getLocation();
} else {
blockPos = new Vec3(
Math.round(hitResult.getLocation().x * alignment) / (double) alignment,
Math.round(hitResult.getLocation().y * alignment) / (double) alignment,
Math.round(hitResult.getLocation().z * alignment) / (double) alignment
);
}

HitResult hitResult = customRaycast(user, 128.0D, 0.0F);
if (hitResult.getType() == HitResult.Type.BLOCK) {
normal = ((BlockHitResult) hitResult).getDirection().getOpposite().getNormal();
if (normal.getY() == 0) {
up = new Vec3i(0, 1, 0);
} else {
final Vec3 lookAngle = user.getLookAngle();
up = Direction.getNearest(lookAngle.x, 0, lookAngle.z).getNormal();
}
right = up.cross(normal);

final int alignment = world.getGameRules().getInt(PortalCubedGameRules.PORTAL_ALIGNMENT);
if (alignment == 0) {
blockPos = hitResult.getLocation();
} else {
blockPos = new Vec3(
Math.round(hitResult.getLocation().x * alignment) / (double) alignment,
Math.round(hitResult.getLocation().y * alignment) / (double) alignment,
Math.round(hitResult.getLocation().z * alignment) / (double) alignment
);
Vec3 portalPos1 = calcPos(blockPos, normal);

assert portalHolder != null;
portalHolder.setDisableValidation(level.getGameRules().getBoolean(PortalCubedGameRules.DISABLE_PORTAL_VALIDATION));
portalHolder.setOwnerUUID(Optional.of(user.getUUID()));
portalHolder.setOriginPos(portalPos1);
portalHolder.setDestination(Optional.of(portalPos1));

portalHolder.setRotation(
IPQuaternion.matrixToQuaternion(
Vec3.atLowerCornerOf(right),
Vec3.atLowerCornerOf(up),
Vec3.atLowerCornerOf(normal)
).toQuaternionf()
);
portalHolder.setColor(this.getSidedColor(stack));

//noinspection DataFlowIssue
final Direction.Axis hAxis = Direction.fromDelta(right.getX(), right.getY(), right.getZ()).getAxis();
findCorrectOrientation:
if (!portalHolder.validate()) {
for (final var try_ : FAIL_TRIES.get(Pair.of(up, right))) {
Vec3 tryPos = portalPos1;
for (final Direction part : try_) {
double newAxis = FAIL_AXIS_DIRS.get(part.getAxisDirection()).get(tryPos.get(part.getAxis()));
if (part.getAxis() == hAxis) {
newAxis += part.getAxisDirection() == Direction.AxisDirection.POSITIVE ? -0.5 : 0.5;
}
tryPos = tryPos.with(part.getAxis(), newAxis);
}
portalHolder.setOriginPos(tryPos);
if (portalHolder.validate()) {
break findCorrectOrientation;
}
}
level.playSound(null, user.position().x(), user.position().y(), user.position().z(), PortalCubedSounds.INVALID_PORTAL_EVENT, SoundSource.NEUTRAL, 1F, 1F);
return;
}

Vec3 portalPos1 = calcPos(blockPos, normal);

assert portalHolder != null;
portalHolder.setOwnerUUID(Optional.of(user.getUUID()));
portalHolder.setOriginPos(portalPos1);
portalHolder.setDestination(Optional.of(portalPos1));

portalHolder.setRotation(
IPQuaternion.matrixToQuaternion(
Vec3.atLowerCornerOf(right),
Vec3.atLowerCornerOf(up),
Vec3.atLowerCornerOf(normal)
).toQuaternionf()
);
portalHolder.setColor(this.getSidedColor(stack));

//noinspection DataFlowIssue
final Direction.Axis hAxis = Direction.fromDelta(right.getX(), right.getY(), right.getZ()).getAxis();
findCorrectOrientation:
if (!portalHolder.validate()) {
for (final var try_ : FAIL_TRIES.get(Pair.of(up, right))) {
Vec3 tryPos = portalPos1;
for (final Direction part : try_) {
double newAxis = FAIL_AXIS_DIRS.get(part.getAxisDirection()).get(tryPos.get(part.getAxis()));
if (part.getAxis() == hAxis) {
newAxis += part.getAxisDirection() == Direction.AxisDirection.POSITIVE ? -0.5 : 0.5;
}
tryPos = tryPos.with(part.getAxis(), newAxis);
}
portalHolder.setOriginPos(tryPos);
if (portalHolder.validate()) {
break findCorrectOrientation;
final List<Portal> overlappingPortals = level.getEntities(
PortalCubedEntities.PORTAL,
portalHolder.getBoundingBox(),
p -> p != originalPortal && vectorsEqual(p.getNormal(), portalHolder.getNormal())
);

if (!overlappingPortals.isEmpty()) {
boolean bumpSuccess = false;
if (overlappingPortals.size() == 1) {
final Portal overlappingPortal = overlappingPortals.get(0);
if (overlappingPortal.getAxisW().equals(portalHolder.getAxisW())) {
final Direction.Axis axis = Objects.requireNonNull(Direction.fromDelta(right.getX(), right.getY(), right.getZ())).getAxis();
if (overlappingPortal.getOriginPos().get(axis) < portalHolder.getOriginPos().get(axis)) {
portalHolder.setOriginPos(portalHolder.getOriginPos().with(axis, overlappingPortal.getOriginPos().get(axis) + 1));
} else {
portalHolder.setOriginPos(portalHolder.getOriginPos().with(axis, overlappingPortal.getOriginPos().get(axis) - 1));
}
bumpSuccess = portalHolder.validate();
}
world.playSound(null, user.position().x(), user.position().y(), user.position().z(), PortalCubedSounds.INVALID_PORTAL_EVENT, SoundSource.NEUTRAL, 1F, 1F);
}
if (!bumpSuccess) {
level.playSound(null, user.position().x(), user.position().y(), user.position().z(), PortalCubedSounds.INVALID_PORTAL_EVENT, SoundSource.NEUTRAL, 1F, 1F);
return;
}
}

final List<Portal> overlappingPortals = world.getEntities(
PortalCubedEntities.PORTAL,
portalHolder.getBoundingBox(),
p -> p != originalPortal && vectorsEqual(p.getNormal(), portalHolder.getNormal())
);
if (originalPortal == null) {
portalHolder.setLinkedPortalUUID(Optional.empty());
} else {
CalledValues.removePortals(user, originalPortal.getUUID());
originalPortal.kill();
}
level.addFreshEntity(portalHolder);
CalledValues.addPortals(user, portalHolder.getUUID());
final boolean isOtherAuto = otherPortal == null;
if (isOtherAuto) {
otherPortal = getPotentialOpposite(
level, portalPos1, portalHolder, portalHolder.getColor(), allowLinkingToOther()
).orElse(null);
}
if (otherPortal != null) {
linkPortals(portalHolder, otherPortal, 0.1f);

if (!overlappingPortals.isEmpty()) {
boolean bumpSuccess = false;
if (overlappingPortals.size() == 1) {
final Portal overlappingPortal = overlappingPortals.get(0);
if (overlappingPortal.getAxisW().equals(portalHolder.getAxisW())) {
final Direction.Axis axis = Objects.requireNonNull(Direction.fromDelta(right.getX(), right.getY(), right.getZ())).getAxis();
if (overlappingPortal.getOriginPos().get(axis) < portalHolder.getOriginPos().get(axis)) {
portalHolder.setOriginPos(portalHolder.getOriginPos().with(axis, overlappingPortal.getOriginPos().get(axis) + 1));
} else {
portalHolder.setOriginPos(portalHolder.getOriginPos().with(axis, overlappingPortal.getOriginPos().get(axis) - 1));
}
bumpSuccess = portalHolder.validate();
}
}
if (!bumpSuccess) {
world.playSound(null, user.position().x(), user.position().y(), user.position().z(), PortalCubedSounds.INVALID_PORTAL_EVENT, SoundSource.NEUTRAL, 1F, 1F);
return;
}
portalHolder.setOwnerUUID(Optional.of(user.getUUID()));
if (!isOtherAuto) {
otherPortal.setOwnerUUID(Optional.of(user.getUUID()));
}

if (originalPortal == null) {
portalHolder.setLinkedPortalUUID(Optional.empty());
} else {
CalledValues.removePortals(user, originalPortal.getUUID());
originalPortal.kill();
}
world.addFreshEntity(portalHolder);
CalledValues.addPortals(user, portalHolder.getUUID());
final boolean isOtherAuto = otherPortal == null;
if (isOtherAuto) {
otherPortal = getPotentialOpposite(
world, portalPos1, portalHolder, portalHolder.getColor(), allowLinkingToOther()
).orElse(null);
}
if (otherPortal != null) {
linkPortals(portalHolder, otherPortal, 0.1f);

portalHolder.setOwnerUUID(Optional.of(user.getUUID()));
if (!isOtherAuto) {
otherPortal.setOwnerUUID(Optional.of(user.getUUID()));
}

CalledValues.addPortals(user, portalHolder.getUUID());
if (!isOtherAuto) {
CalledValues.addPortals(user, otherPortal.getUUID());
}
if (!isOtherAuto) {
CalledValues.addPortals(user, otherPortal.getUUID());
}
} else {
world.playSound(null, user.position().x(), user.position().y(), user.position().z(), PortalCubedSounds.INVALID_PORTAL_EVENT, SoundSource.NEUTRAL, 1F, 1F);
return;
}

portalsTag.putUUID((leftClick ? "Left" : "Right") + "Portal", portalHolder.getUUID());

tag.put(world.dimension().location().toString(), portalsTag);
tag.put(level.dimension().location().toString(), portalsTag);
portalHolder.notifyListeners(false);
// also notify the other portal to re-propagate
Portal linked = portalHolder.findLinkedPortal();
if (linked != null)
linked.notifyListeners(false);
if (otherPortal != null)
otherPortal.notifyListeners(false);
} else {
final LocalPlayer localPlayer = (LocalPlayer)user;
if (localPlayer.input.getMoveVector().lengthSquared() < 0.1 && user.getXRot() >= 88.0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public PortalGunPrimary(Properties settings) {
}

@Override
protected void shoot(Level world, Player user, InteractionHand hand, boolean leftClick) {
super.shoot(world, user, hand, true);
protected void shoot(Level level, Player user, InteractionHand hand, boolean leftClick) {
super.shoot(level, user, hand, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public PortalGunSecondary(Properties settings) {
}

@Override
protected void shoot(Level world, Player user, InteractionHand hand, boolean leftClick) {
super.shoot(world, user, hand, false);
protected void shoot(Level level, Player user, InteractionHand hand, boolean leftClick) {
super.shoot(level, user, hand, false);
}

@Override
Expand Down

0 comments on commit 54d0904

Please sign in to comment.