Skip to content

Commit

Permalink
Merge pull request #216 from Gu-ZT/fix#181
Browse files Browse the repository at this point in the history
让空心磁铁充能后不会转化磁铁
  • Loading branch information
XeKr authored Apr 11, 2024
2 parents a7d2b5d + d993547 commit 7f90dec
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.mixin;

import dev.dubhe.anvilcraft.block.HollowMagnetBlock;
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.init.ModItems;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -9,42 +10,52 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemEntity.class)
abstract class ItemEntityMixin extends Entity {
@Shadow public abstract ItemStack getItem();

@Shadow @Nullable public abstract Entity getOwner();

@Shadow public abstract void setItem(ItemStack stack);

public ItemEntityMixin(EntityType<?> entityType, Level level) {
super(entityType, level);
}

@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/phys/Vec3;add(DDD)Lnet/minecraft/world/phys/Vec3;"))
private Vec3 slowDown(Vec3 instance, double dx, double dy, double dz) {
ItemEntity ths = (ItemEntity) (Object) this;
if (ths.level().getBlockState(ths.blockPosition()).is(ModBlocks.HOLLOW_MAGNET_BLOCK.get()))
if (this.level().getBlockState(this.blockPosition()).is(ModBlocks.HOLLOW_MAGNET_BLOCK.get()))
dy *= 0.2;
return instance.add(dx, dy, dz);
}

private boolean needMagnetization = true;
@Unique
private boolean anvilcraft$needMagnetization = true;

@Inject(method = "tick", at = @At(value = "HEAD"))
private void magnetization(CallbackInfo ci){
private void magnetization(CallbackInfo ci) {
if (this.getServer() == null) return;
ItemEntity ths = (ItemEntity) (Object) this;
if (!ths.level().getBlockState(ths.blockPosition()).is(ModBlocks.HOLLOW_MAGNET_BLOCK.get())) return;
if (ths.getOwner() == null || !(ths.getOwner() instanceof ServerPlayer)) return;
ItemStack itemStack = ths.getItem();
ItemStack itemStack = this.getItem();
if (!itemStack.is(Items.IRON_INGOT)) return;
BlockState blockState = this.level().getBlockState(this.blockPosition());
if (!blockState.is(ModBlocks.HOLLOW_MAGNET_BLOCK.get()) || blockState.getValue(HollowMagnetBlock.LIT)) return;
if (this.getOwner() == null || !(this.getOwner() instanceof ServerPlayer)) return;
if (itemStack.getCount() != 1) return;
if (!this.needMagnetization) return;
this.needMagnetization = false;
int random = (int) (Math.random() * 20);
if (random == 0){
ths.setItem(new ItemStack(ModItems.MAGNET_INGOT));
if (!this.anvilcraft$needMagnetization) return;
this.anvilcraft$needMagnetization = false;
if (this.level().random.nextInt(100) <= 5) {
this.setItem(new ItemStack(ModItems.MAGNET_INGOT));
}
}
}

0 comments on commit 7f90dec

Please sign in to comment.