generated from NeoForgeMDKs/MDK-1.21-ModDevGradle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
509 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.devdyna.btw_ores; | ||
|
||
import com.devdyna.btw_ores.events.BlockBreak; | ||
import com.devdyna.btw_ores.registry.ItemsBlocks; | ||
import com.devdyna.btw_ores.registry.Tab; | ||
import net.neoforged.bus.api.IEventBus; | ||
import net.neoforged.fml.ModContainer; | ||
import net.neoforged.fml.common.Mod; | ||
import net.neoforged.neoforge.common.NeoForge; | ||
|
||
@Mod(Main.MODID) | ||
public class Main { | ||
|
||
public static final String MODID = "btw_ores"; | ||
|
||
public Main(IEventBus modEventBus, ModContainer modContainer) { | ||
Tab.register(modEventBus); | ||
ItemsBlocks.register(modEventBus); | ||
NeoForge.EVENT_BUS.register(new BlockBreak()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.devdyna.btw_ores; | ||
|
||
import java.util.List; | ||
import java.util.Random; | ||
|
||
|
||
|
||
// import org.slf4j.Logger; | ||
|
||
// import com.mojang.logging.LogUtils; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.enchantment.Enchantment; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.LevelAccessor; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.storage.loot.LootParams; | ||
import net.minecraft.world.level.storage.loot.LootTable; | ||
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets; | ||
import net.neoforged.neoforge.registries.DeferredBlock; | ||
|
||
public class Utils { | ||
|
||
/** | ||
* @param level net.minecraft.world.level.Level | ||
* @param enchant Enchantments.name | ||
* @return Holder[Enchantment] | ||
*/ | ||
public static Holder<Enchantment> getEnchantHolder(LevelAccessor level, ResourceKey<Enchantment> enchant) { | ||
return level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT) | ||
.getOrThrow(enchant); | ||
} | ||
|
||
// private static final Logger LOGGER = LogUtils.getLogger(); | ||
|
||
@SuppressWarnings("null") | ||
public static List<ItemStack> getItemStackFromLootTable(LevelAccessor level, Player player, String raw_ore_name) { | ||
|
||
LootParams.Builder builder = new LootParams.Builder((ServerLevel) level); | ||
LootParams params = builder.create(LootContextParamSets.EMPTY); | ||
builder.withLuck(player.getLuck()); | ||
|
||
LootTable lootTable = level.getServer().reloadableRegistries().getLootTable(ResourceKey | ||
.create(Registries.LOOT_TABLE, ResourceLocation.fromNamespaceAndPath( | ||
getModName(raw_ore_name), "blocks/" | ||
+ raw_ore_name.substring(raw_ore_name.lastIndexOf('.') + 1)))); | ||
return lootTable.getRandomItems(params); | ||
|
||
} | ||
|
||
@SuppressWarnings({ "resource", "null" }) | ||
public void messageActionBar(String name) { | ||
Minecraft.getInstance().player.displayClientMessage(Component.literal(name), | ||
true); | ||
} | ||
|
||
public static int getRandomValue(int value) { | ||
if (value == 0) { | ||
return 1; | ||
} | ||
|
||
Random random = new Random(); | ||
return random.nextInt(value) + 1; | ||
} | ||
|
||
public static String getModName(String traslationName) { | ||
String[] parts = traslationName.split("\\."); | ||
if (parts.length >= 2) { | ||
return parts[1]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public static boolean isDimension(Player player ,ResourceKey<Level> dim | ||
){ | ||
return player.level().dimension().equals(dim); | ||
} | ||
|
||
public static void SimplePlaceBlock(LevelAccessor levelAccessor,BlockPos pos,DeferredBlock<Block> block){ | ||
levelAccessor.setBlock(pos, block.get().defaultBlockState(), 32); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.devdyna.btw_ores.events; | ||
|
||
import java.util.List; | ||
|
||
import com.devdyna.btw_ores.Utils; | ||
import com.devdyna.btw_ores.registry.ItemsBlocks; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.entity.item.ItemEntity; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.enchantment.Enchantments; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.LevelAccessor; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.neoforge.common.Tags; | ||
import net.neoforged.neoforge.event.level.BlockEvent; | ||
import net.neoforged.neoforge.event.level.BlockEvent.BreakEvent; | ||
import net.neoforged.neoforge.registries.DeferredBlock; | ||
|
||
public class BlockBreak { | ||
|
||
@SubscribeEvent | ||
public void BlockBreakEvent(BlockEvent.BreakEvent event) { | ||
|
||
LevelAccessor levelAccessor = event.getLevel(); | ||
BlockPos pos = event.getPos(); | ||
Player player = event.getPlayer(); | ||
BlockState state = event.getState(); | ||
String raw_ore_name = state.getBlock().getDescriptionId(); | ||
|
||
if (state.is(Tags.Blocks.ORES) && !state.is(com.devdyna.btw_ores.registry.AnyTags.BLACKLISTED_ORES) | ||
&& player.getMainHandItem() | ||
.getEnchantmentLevel(Utils.getEnchantHolder(levelAccessor, Enchantments.SILK_TOUCH)) == 0) { | ||
|
||
// ore broken on overworld with stone tag over 0 | ||
if (Utils.isDimension(player, Level.OVERWORLD) && state.is(Tags.Blocks.ORES_IN_GROUND_STONE) | ||
&& pos.getY() >= 0) { | ||
Utils.SimplePlaceBlock(levelAccessor, pos, ItemsBlocks.STONE_CLUSTER_BLOCK); | ||
DropItems(event, raw_ore_name, ItemsBlocks.STONE_CLUSTER_BLOCK); | ||
|
||
} | ||
// ore broken on overworld with deepslate tag below 0 | ||
if (Utils.isDimension(player, Level.OVERWORLD) && state.is(Tags.Blocks.ORES_IN_GROUND_DEEPSLATE) | ||
&& pos.getY() < 0) { | ||
DropItems(event, raw_ore_name, ItemsBlocks.DEEP_CLUSTER_BLOCK); | ||
|
||
} | ||
// ore broken on nether with nether tag | ||
if (Utils.isDimension(player, Level.NETHER) && state.is(Tags.Blocks.ORES_IN_GROUND_NETHERRACK)) { | ||
DropItems(event, raw_ore_name, ItemsBlocks.NETHER_CLUSTER_BLOCK); | ||
} | ||
// ore broken on end with custom end tag with "end" without "nether" & "stone" & | ||
// "deepslate" | ||
if (Utils.isDimension(player, Level.END) | ||
&& state.is(com.devdyna.btw_ores.registry.AnyTags.ORES_IN_GROUND_END) | ||
&& raw_ore_name.contains("end") | ||
&& !raw_ore_name.contains("nether") && !raw_ore_name.contains("deepslate") | ||
&& !raw_ore_name.contains("stone")) { | ||
DropItems(event, raw_ore_name, ItemsBlocks.END_CLUSTER_BLOCK); | ||
} | ||
// ore broken on other dimension | ||
if (!Utils.isDimension(player, Level.OVERWORLD) && !Utils.isDimension(player, Level.NETHER) | ||
&& !Utils.isDimension(player, Level.END)) { | ||
DropItems(event, raw_ore_name, ItemsBlocks.NULL_CLUSTER_BLOCK); | ||
} | ||
} | ||
|
||
} | ||
|
||
private void DropItems(BreakEvent event, String raw_ore_name, DeferredBlock<Block> block) { | ||
for (int i = 0; i < Utils.getRandomValue(event.getPlayer().getMainHandItem() | ||
.getEnchantmentLevel(Utils.getEnchantHolder(event.getLevel(), Enchantments.FORTUNE))); i++) { | ||
|
||
List<ItemStack> list = Utils.getItemStackFromLootTable(event.getLevel(), event.getPlayer(), raw_ore_name); | ||
|
||
for (ItemStack itemStack : list) { | ||
ItemEntity itementity = new ItemEntity((net.minecraft.world.level.Level) event.getLevel(), | ||
event.getPos().getX(), | ||
event.getPos().getY(), | ||
event.getPos().getZ(), | ||
itemStack); | ||
event.getLevel().addFreshEntity(itementity); | ||
} | ||
event.setCanceled(true); | ||
} | ||
Utils.SimplePlaceBlock(event.getLevel(), event.getPos(), block); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.devdyna.btw_ores.registry; | ||
|
||
import com.devdyna.btw_ores.Main; | ||
|
||
import net.minecraft.core.registries.BuiltInRegistries; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.tags.TagKey; | ||
import net.minecraft.world.level.block.Block; | ||
|
||
public class AnyTags { | ||
|
||
AnyTags() { | ||
} | ||
|
||
|
||
public static final TagKey<Block> BLACKLISTED_ORES = TagKey.create(BuiltInRegistries.BLOCK.key(), | ||
ResourceLocation.fromNamespaceAndPath(Main.MODID, "cannot_spawn_ore_cluster")); | ||
|
||
public static final TagKey<Block> ORES_IN_GROUND_END = TagKey.create(BuiltInRegistries.BLOCK.key(), | ||
ResourceLocation.fromNamespaceAndPath("c", "ores_in_ground/end")); | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/devdyna/btw_ores/registry/ItemsBlocks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.devdyna.btw_ores.registry; | ||
|
||
import com.devdyna.btw_ores.Main; | ||
|
||
import net.minecraft.world.item.BlockItem; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.state.BlockBehaviour; | ||
import net.neoforged.bus.api.IEventBus; | ||
import net.neoforged.neoforge.registries.DeferredBlock; | ||
import net.neoforged.neoforge.registries.DeferredItem; | ||
import net.neoforged.neoforge.registries.DeferredRegister; | ||
|
||
public class ItemsBlocks { | ||
|
||
ItemsBlocks() { | ||
} | ||
|
||
public static void register(IEventBus bus) { | ||
ITEMS.register(bus); | ||
BLOCKS.register(bus); | ||
} | ||
|
||
public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(Main.MODID); | ||
public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(Main.MODID); | ||
|
||
public static final DeferredBlock<Block> STONE_CLUSTER_BLOCK = ClusterBlock("stone"); | ||
public static final DeferredBlock<Block> DEEP_CLUSTER_BLOCK = ClusterBlock("deep"); | ||
public static final DeferredBlock<Block> NETHER_CLUSTER_BLOCK = ClusterBlock("nether"); | ||
public static final DeferredBlock<Block> END_CLUSTER_BLOCK = ClusterBlock("end"); | ||
public static final DeferredBlock<Block> NULL_CLUSTER_BLOCK = ClusterBlock("null"); | ||
|
||
|
||
|
||
public static final DeferredItem<BlockItem> STONE_CLUSTER_ITEM = ClusterItem(STONE_CLUSTER_BLOCK); | ||
public static final DeferredItem<BlockItem> DEEP_CLUSTER_ITEM = ClusterItem( DEEP_CLUSTER_BLOCK); | ||
public static final DeferredItem<BlockItem> NETHER_CLUSTER_ITEM = ClusterItem( NETHER_CLUSTER_BLOCK); | ||
public static final DeferredItem<BlockItem> END_CLUSTER_ITEM = ClusterItem( END_CLUSTER_BLOCK); | ||
public static final DeferredItem<BlockItem> NULL_CLUSTER_ITEM = ClusterItem( NULL_CLUSTER_BLOCK); | ||
|
||
public static DeferredBlock<Block> ClusterBlock(String name) { | ||
return BLOCKS.registerSimpleBlock( name+"_cluster", | ||
BlockBehaviour.Properties.of().destroyTime(10).explosionResistance(100)); | ||
} | ||
|
||
|
||
public static DeferredItem<BlockItem> ClusterItem(DeferredBlock<Block> block ) { | ||
return ITEMS.registerSimpleBlockItem(block); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.devdyna.btw_ores.registry; | ||
|
||
import com.devdyna.btw_ores.Main; | ||
|
||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.world.item.CreativeModeTab; | ||
import net.minecraft.world.item.CreativeModeTabs; | ||
import net.neoforged.bus.api.IEventBus; | ||
import net.neoforged.neoforge.registries.DeferredHolder; | ||
import net.neoforged.neoforge.registries.DeferredRegister; | ||
|
||
public class Tab { | ||
|
||
Tab() { | ||
} | ||
|
||
public static void register(IEventBus bus) { | ||
CREATIVE_MODE_TABS.register(bus); | ||
} | ||
|
||
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister | ||
.create(Registries.CREATIVE_MODE_TAB, Main.MODID); | ||
public static final DeferredHolder<CreativeModeTab, CreativeModeTab> CLUSTER_TAB = CREATIVE_MODE_TABS | ||
.register("cluster_tab", () -> CreativeModeTab.builder() | ||
.title(Component.translatable(Main.MODID + ".cluster.tab")) | ||
.withTabsBefore(CreativeModeTabs.COMBAT) | ||
.icon(() -> ItemsBlocks.STONE_CLUSTER_ITEM.get().getDefaultInstance()) | ||
.displayItems((parameters, output) -> { | ||
output.accept(ItemsBlocks.STONE_CLUSTER_BLOCK.get()); | ||
output.accept(ItemsBlocks.DEEP_CLUSTER_BLOCK.get()); | ||
output.accept(ItemsBlocks.NETHER_CLUSTER_BLOCK.get()); | ||
output.accept(ItemsBlocks.END_CLUSTER_BLOCK.get()); | ||
output.accept(ItemsBlocks.NULL_CLUSTER_BLOCK.get()); | ||
}).build()); | ||
} |
Oops, something went wrong.