Skip to content

Commit

Permalink
fixed magnet player hovering
Browse files Browse the repository at this point in the history
fixed magnet config crash
fixed magnet particles disappearing when picking up blocks
stonecutter magnet action will now silk touch blocks
  • Loading branch information
MehVahdJukaar committed May 28, 2024
1 parent 3d8e594 commit 320f02b
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.FallingBlockEntity;
Expand All @@ -18,6 +19,7 @@
import org.violetmoon.quark.addons.oddities.magnetsystem.MagnetSystem;
import org.violetmoon.quark.addons.oddities.module.MagnetsModule;
import org.violetmoon.quark.api.IMagneticEntity;
import org.violetmoon.quark.mixin.mixins.accessor.AccessorServerGamePacketListener;
import org.violetmoon.zeta.api.ICollateralMover;

public class MagnetBlockEntity extends BlockEntity {
Expand Down Expand Up @@ -78,10 +80,10 @@ private void magnetize(BlockState state, Direction dir, Direction moveDir, int p
}

//TODO: move this into magnet system. although might not be needed as there it only serves since directions must be discrete
if (!level.isClientSide && MagnetsModule.affectEntities && i > 1) {
if (MagnetsModule.affectEntities && i > 1) {

var entities = level.getEntities((Entity) null, new AABB(worldPosition)
.expandTowards(new Vec3(dir.step().mul(i))), this::canPullEntity);
.expandTowards(new Vec3(dir.step().mul(i))), this::canPullEntity);
for (Entity e : entities) {
double distanceFromMagnetSq = e.distanceToSqr(worldPosition.getCenter());
double invSquared = 1 / distanceFromMagnetSq;
Expand All @@ -91,33 +93,39 @@ private void magnetize(BlockState state, Direction dir, Direction moveDir, int p
me.moveByMagnet(e, vec, this);
} else {
e.push(vec.x(), vec.y(), vec.z());
if (e instanceof Player player) {
//should probably send a packet here actually
player.hurtMarked = true;
if (e instanceof ServerPlayer player) {
//reset flying kick time
((AccessorServerGamePacketListener) player.connection).setAboveGroundTickCount(0);
} else {
//hurt mark everybody but the player. its handled by client side code
e.hurtMarked = true;
}
if(e instanceof FallingBlockEntity fb){
if (e instanceof FallingBlockEntity fb) {
fb.time--;
fb.hurtMarked = true;
//hack.
}
e.fallDistance = 0;
}
}
}
}

private boolean canPullEntity(Entity e){
private boolean canPullEntity(Entity e) {
if (this.level.isClientSide) return e instanceof Player;
if (e instanceof IMagneticEntity) return true;
if (e.getType().is(MagnetsModule.magneticEntities)) return true;

if (e instanceof ItemEntity ie){
if (e instanceof ItemEntity ie) {
return MagnetSystem.isItemMagnetic(ie.getItem().getItem());
}
if (e instanceof FallingBlockEntity fb){

if (e.getType().is(MagnetsModule.magneticEntities)) return true;

if (e instanceof FallingBlockEntity fb) {
return MagnetSystem.isBlockMagnetic(fb.getBlockState());
}

if (MagnetsModule.affectsArmor){
for (var armor : e.getArmorSlots()){
if (MagnetsModule.affectsArmor) {
for (var armor : e.getArmorSlots()) {
if (MagnetSystem.isItemMagnetic(armor.getItem())) return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.*;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleGroup;
import net.minecraft.core.particles.SimpleParticleType;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
Expand All @@ -15,6 +16,7 @@
import org.violetmoon.quark.addons.oddities.module.MagnetsModule;

import java.util.List;
import java.util.Optional;

public class MagnetParticle extends TextureSheetParticle {

Expand Down Expand Up @@ -104,7 +106,6 @@ public void tick() {
this.yWobbleO = this.yWobble;
this.xWobble = random.nextFloat() * wobbleAmount;
this.yWobble = random.nextFloat() * wobbleAmount;

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.ItemPickupParticle;
import net.minecraft.client.particle.ParticleEngine;
import net.minecraft.client.particle.ParticleRenderType;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RegisterParticleProvidersEvent;
import net.minecraftforge.client.event.RegisterShadersEvent;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.client.gui.overlay.VanillaGuiOverlay;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.lwjgl.opengl.GL13;
import org.violetmoon.quark.addons.oddities.module.MagnetsModule;
import org.violetmoon.quark.base.Quark;

Expand All @@ -33,8 +41,12 @@ public class MagnetParticleRenderType {
public static final ParticleRenderType ADDITIVE_TRANSLUCENCY = new ParticleRenderType() {
@Override
public void begin(BufferBuilder builder, TextureManager textureManager) {
RenderSystem.setShader(PARTICLE_SHADER);
Minecraft.getInstance().gameRenderer.lightTexture().turnOnLightLayer();
RenderSystem.activeTexture(GL13.GL_TEXTURE2);
RenderSystem.activeTexture(GL13.GL_TEXTURE0);

RenderSystem.depthMask(false);
RenderSystem.setShader(PARTICLE_SHADER);
RenderSystem.setShaderTexture(0, TextureAtlas.LOCATION_PARTICLES);
RenderSystem.enableBlend();
RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE);
Expand Down Expand Up @@ -71,5 +83,16 @@ public static void registerParticleProviders(RegisterParticleProvidersEvent even
event.registerSpriteSet(MagnetsModule.attractorParticle, MagnetParticle.Provider::new);
event.registerSpriteSet(MagnetsModule.repulsorParticle, MagnetParticle.Provider::new);
}
/*
static{
MinecraftForge.EVENT_BUS.addListener(MagnetParticleRenderType::renderParticlesAfterEverything);
}
public static void renderParticlesAfterEverything(RenderLevelStageEvent event){
if(event.getStage() == RenderLevelStageEvent.Stage.AFTER_SKY){
}
}*/

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package org.violetmoon.quark.addons.oddities.magnetsystem;

import java.util.HashMap;

import java.util.Map;
import java.util.UUID;

import com.mojang.authlib.GameProfile;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.material.FluidState;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory;
import org.violetmoon.quark.addons.oddities.module.MagnetsModule;
import org.violetmoon.quark.api.IMagnetMoveAction;

import net.minecraft.core.BlockPos;
Expand All @@ -11,17 +26,15 @@
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.DoublePlantBlock;
import net.minecraft.world.level.block.HopperBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.IPlantable;

import javax.annotation.Nullable;

public class DefaultMoveActions {

public static void addActions(HashMap<Block, IMagnetMoveAction> map) {
Expand All @@ -30,15 +43,51 @@ public static void addActions(HashMap<Block, IMagnetMoveAction> map) {
}

private static void stonecutterMoved(Level world, BlockPos pos, Direction direction, BlockState state, BlockEntity tile) {
if(!world.isClientSide) {
if(world instanceof ServerLevel serverLevel) {
BlockPos up = pos.above();
BlockState breakState = world.getBlockState(up);
double hardness = breakState.getDestroySpeed(world, up);
if(hardness > -1 && hardness < 3)
world.destroyBlock(up, true);
if(hardness > -1 && hardness < 3) {
if (MagnetsModule.stoneCutterSilkTouch) {
destroyBlockWithSilkTouch(breakState, up, serverLevel, 512);
}else{
world.destroyBlock(up, true);
}
}
}
}

private static final GameProfile FAKE_PLAYER_PROFILE = new GameProfile(UUID.randomUUID(), "[MagnetStonecutter]");

//Like level.removeBlock buth with skil touch
private static boolean destroyBlockWithSilkTouch(BlockState blockstate, BlockPos pPos, ServerLevel level, int pRecursionLeft) {
if (blockstate.isAir()) {
return false;
} else {
FluidState fluidstate = level.getFluidState(pPos);
if (!(blockstate.getBlock() instanceof BaseFireBlock)) {
level.levelEvent(2001, pPos, Block.getId(blockstate));
}

FakePlayer player = FakePlayerFactory.get(level, FAKE_PLAYER_PROFILE);
ItemStack tool = Items.NETHERITE_PICKAXE.getDefaultInstance();
EnchantmentHelper.setEnchantments(Map.of(Enchantments.SILK_TOUCH, 1), tool);
player.setItemInHand(InteractionHand.MAIN_HAND, tool);

BlockEntity blockentity = blockstate.hasBlockEntity() ? level.getBlockEntity(pPos) : null;
Block.dropResources(blockstate, level, pPos, blockentity, player, tool);


boolean flag = level.setBlock(pPos, fluidstate.createLegacyBlock(), 3, pRecursionLeft);
if (flag) {
level.gameEvent(GameEvent.BLOCK_DESTROY, pPos, GameEvent.Context.of(null, blockstate));
}

return flag;
}
}


private static void hopperMoved(Level world, BlockPos pos, Direction direction, BlockState state, BlockEntity tile) {
if(!world.isClientSide && tile instanceof HopperBlockEntity hopper) {
hopper.setCooldown(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,18 @@ public static void onDigest() {
});

//...in favor of manual fixup
List<Block> magneticWhitelist = RegistryUtil.massRegistryGet(MagnetsModule.magneticWhitelist, BuiltInRegistries.BLOCK);
List<Block> magneticBlacklist = RegistryUtil.massRegistryGet(MagnetsModule.magneticBlacklist, BuiltInRegistries.BLOCK);
List<Block> magneticBlockWhitelist = RegistryUtil.massRegistryGet(MagnetsModule.magneticWhitelist, BuiltInRegistries.BLOCK);
List<Block> magneticBlockBlacklist = RegistryUtil.massRegistryGet(MagnetsModule.magneticBlacklist, BuiltInRegistries.BLOCK);

magnetizableBlocks.addAll(magneticWhitelist);
magneticBlacklist.forEach(magnetizableBlocks::remove);
magnetizableBlocks.addAll(magneticBlockWhitelist);
magneticBlockBlacklist.forEach(magnetizableBlocks::remove);

//...and manual fixup for items
List<Item> magneticItemWhitelist = RegistryUtil.massRegistryGet(MagnetsModule.magneticWhitelist, BuiltInRegistries.ITEM);
List<Item> magneticItemBlacklist = RegistryUtil.massRegistryGet(MagnetsModule.magneticBlacklist, BuiltInRegistries.ITEM);

magnetizableItems.addAll(magneticItemWhitelist);
magneticItemBlacklist.forEach(magnetizableItems::remove);
}

public static void applyForce(Level world, BlockPos pos, int magnitude, boolean pushing, Direction dir, int distance, BlockPos origin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public class MagnetsModule extends ZetaModule {
@Config(description = "Any items you place in this list will be derived so that any block made of it will become magnetizable")
public static List<String> magneticDerivationList = Lists.newArrayList("minecraft:iron_ingot", "minecraft:copper_ingot", "minecraft:exposed_copper", "minecraft:weathered_copper", "minecraft:oxidized_copper", "minecraft:raw_iron", "minecraft:raw_copper", "minecraft:iron_ore", "minecraft:deepslate_iron_ore", "minecraft:copper_ore", "minecraft:deepslate_copper_ore", "quark:gravisand");

@Config(description = "Block IDs to force-allow magnetism on, regardless of their crafting recipe")
public static List<String> magneticWhitelist = Lists.newArrayList("minecraft:chipped_anvil", "minecraft:damaged_anvil");
@Config(description = "Block/Item IDs to force-allow magnetism on, regardless of their crafting recipe")
public static List<String> magneticWhitelist = Lists.newArrayList("minecraft:chipped_anvil", "minecraft:damaged_anvil", "minecraft:iron_horse_armor","minecraft:chainmail_helmet", "minecraft:chainmail_boots", "minecraft:chainmail_leggins", "minecraft:chainmail_chestplate");

@Config(description = "Block IDs to force-disable magnetism on, regardless of their crafting recipe")
@Config(description = "Block/Item IDs to force-disable magnetism on, regardless of their crafting recipe")
public static List<String> magneticBlacklist = Lists.newArrayList("minecraft:tripwire_hook");

@Config(flag = "magnet_pre_end")
Expand All @@ -62,6 +62,9 @@ public class MagnetsModule extends ZetaModule {
@Config(description = "Determines how fast entities are pulled by magnets. Still follows the inverse square law")
public static double entitiesPullForce = 0.15f;

@Config(description = "Stonecutters pulled by magnets will silk touch the blocks they cut.")
public static boolean stoneCutterSilkTouch = true;

public static final TagKey<EntityType<?>> magneticEntities = TagKey.create(Registries.ENTITY_TYPE, Quark.asResource("affected_by_magnets"));

@Hint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.violetmoon.zeta.module.ZetaModule;
import org.violetmoon.zeta.util.Hint;

@ZetaLoadModule(category = "building")
@ZetaLoadModule(category = "building", antiOverlap = {"supplementaries"})
public class RopeModule extends ZetaModule {

@Hint
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.violetmoon.quark.mixin.mixins.accessor;

import net.minecraft.server.network.ServerGamePacketListenerImpl;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ServerGamePacketListenerImpl.class)
public interface AccessorServerGamePacketListener {

@Accessor("aboveGroundTickCount")
void setAboveGroundTickCount(int t);

}
1 change: 1 addition & 0 deletions src/main/resources/quark.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"accessor.AccessorAbstractChestedHorse",
"accessor.AccessorLivingEntity",
"accessor.AccessorMerchantOffer",
"accessor.AccessorServerGamePacketListener",
"accessor.AccessorTemptingSensor",
"self.IZetaBlockMixin_FAKE",
"self.IZetaItemMixin_FAKE"
Expand Down

0 comments on commit 320f02b

Please sign in to comment.