Skip to content

Commit

Permalink
変更
Browse files Browse the repository at this point in the history
  • Loading branch information
PTOM76 committed Jan 17, 2023
1 parent 30776c1 commit 2e05c4e
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.ptom76.tutorialfabricmod;

import com.github.ptom76.tutorialfabricmod.block.Blocks;
import com.github.ptom76.tutorialfabricmod.enchantment.Enchantments;
import com.github.ptom76.tutorialfabricmod.entity.Entities;
import com.github.ptom76.tutorialfabricmod.item.ItemGroups;
import com.github.ptom76.tutorialfabricmod.item.Items;
Expand Down Expand Up @@ -32,6 +33,9 @@ public void onInitialize() {

// クリエイティブタブの登録
ItemGroups.addItems();

// エンチャントの登録
Enchantments.init();
}

// Identifierを生成する
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.ptom76.tutorialfabricmod.enchantment;

import com.github.ptom76.tutorialfabricmod.TutorialFabricMod;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;

public class Enchantments {
public static Enchantment FREEZE = new FreezeEnchantment();

public static void init() {
// エンチャント登録
Registry.register(Registries.ENCHANTMENT, TutorialFabricMod.id("freeze"), FREEZE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.github.ptom76.tutorialfabricmod.enchantment;

import com.github.ptom76.tutorialfabricmod.timer.StuffTimerAccess;
import net.minecraft.block.Blocks;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentTarget;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class FreezeEnchantment extends Enchantment {
public FreezeEnchantment() {
super(Rarity.UNCOMMON, EnchantmentTarget.WEAPON, new EquipmentSlot[]{EquipmentSlot.MAINHAND});
}

@Override
public int getMaxPower(int level) {
return super.getMinPower(level) + 50;
}

@Override
public int getMinPower(int level) {
return level * 10;
}

@Override
public int getMaxLevel() {
return 3;
}

@Override
public void onTargetDamaged(LivingEntity user, Entity target, int level) {
if (target instanceof LivingEntity) {
LivingEntity entity = (LivingEntity) target;
BlockPos pos = entity.getBlockPos();
World world = entity.getWorld();
// ブロックが空気のとき
if (world.getBlockState(pos).isAir()) {
// 音の再生
target.playSound(SoundEvents.BLOCK_GLASS_BREAK, 1f, 1f);

// 薄氷の設置
world.setBlockState(pos, Blocks.ICE.getDefaultState());
}
if (world.getBlockState(pos.up()).isAir()) {
world.setBlockState(pos.up(), Blocks.ICE.getDefaultState());
}

// 移動速度低下の追加
entity.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 500, 255, false, false, false));

StuffTimerAccess stuffTimerAccess = (StuffTimerAccess) world.getServer();

stuffTimerAccess.tutorialFabricMod_addTimer(5, () -> {
// エンティティの場所を設定
entity.teleport(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
return true;
});

stuffTimerAccess.tutorialFabricMod_addTimer(45, () -> {
// ブロックが薄氷のとき、
if (world.getBlockState(pos).getBlock() == Blocks.ICE) {

// ブロック破壊
world.breakBlock(pos, false);

// 攻撃
entity.damage(DamageSource.FREEZE, level * 2);
}
return true;
});
stuffTimerAccess.tutorialFabricMod_addTimer(50, () -> {
// pos.up()はY座標に+1
if (world.getBlockState(pos.up()).getBlock() == Blocks.ICE) {
world.breakBlock(pos.up(), false);
entity.damage(DamageSource.FREEZE, level * 3);
}

// 移動速度低下の除去
entity.removeStatusEffect(StatusEffects.SLOWNESS);

return true;
});


}
super.onTargetDamaged(user, target, level);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.ptom76.tutorialfabricmod.mixin;

import com.github.ptom76.tutorialfabricmod.timer.StuffTimerAccess;
import com.github.ptom76.tutorialfabricmod.timer.TickTimer;
import net.minecraft.server.MinecraftServer;
import org.spongepowered.asm.mixin.Mixin;
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.callback.CallbackInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

@Mixin(MinecraftServer.class)
public class StuffTimer implements StuffTimerAccess {
@Unique
private final List<TickTimer> tickTimers = new ArrayList<>();

@Inject(method = "tick", at = @At("TAIL"))
private void onTick(CallbackInfo ci) {
if (tickTimers.isEmpty()) return;

for (TickTimer tickTimer : tickTimers) {
if (--tickTimer.ticksUntilSomething == 0) {
tickTimer.supplier.get();
}
}

}

@Override
public void tutorialFabricMod_addTimer(long ticksUntilSomething, Supplier<Boolean> supplier) {
tickTimers.add(new TickTimer(ticksUntilSomething, supplier));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.ptom76.tutorialfabricmod.timer;

import java.util.function.Supplier;

public interface StuffTimerAccess {
void tutorialFabricMod_addTimer(long ticksUntilSomething, Supplier<Boolean> supplier);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.ptom76.tutorialfabricmod.timer;

import java.util.function.Supplier;

public class TickTimer {
public long ticksUntilSomething;
public Supplier<Boolean> supplier;

public TickTimer(long ticksUntilSomething, Supplier<Boolean> supplier) {
this.ticksUntilSomething = ticksUntilSomething;
this.supplier = supplier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"block.tutorialfabricmod.tutorial_block": "Tutorial Block",
"block.tutorialfabricmod.click_count_block": "Click Count Block",
"block.tutorialfabricmod.blue_apple_generator": "Blue Apple Generator",
"enchantment.tutorialfabricmod.freeze": "Freeze",

"itemGroup.tutorialfabricmod.tutorial": "Tutorial Mod"
}
1 change: 1 addition & 0 deletions src/main/resources/tutorialfabricmod.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "com.github.ptom76.tutorialfabricmod.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"StuffTimer"
],
"client": [
],
Expand Down

0 comments on commit 2e05c4e

Please sign in to comment.