Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Mod properties
repo=https://github.com/JouTak/TemplateMod-Java
group=ru.joutak
mod_name=TemplateMod-Java
mod_id=templatemod
version=1.0.0
repo=https://github.com/DragonOfVoid/BeaconRadius
group=ru.jor
mod_name=beacon-radius
mod_id=beaconradius
version=0.0.1

# Gradle properties
org.gradle.parallel=true
org.gradle.configuration-cache=true
96 changes: 96 additions & 0 deletions src/main/java/ru/Jor/BeaconRadius/BRStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ru.Jor.BeaconRadius;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.Identifier;
import net.minecraft.world.level.storage.LevelResource;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.lang.reflect.Type;
import java.util.*;

public class BRStorage {
private static final File StorageDir = new File(FabricLoader.getInstance().getGameDir().toFile(),"BeaconRadius");

public static Map<BlockPos, Identifier> blockCache = new HashMap<>();

public static void Toggle(BlockPos block,Identifier dim){
if(blockCache.entrySet().contains((Map.entry(block,dim)))){
blockCache.remove(block,dim);
}else{
blockCache.put(block,dim);
}
save();
}

public static void onJoin()
{
load();
}

private static boolean save(){
File worldStorage = FileName();

if(worldStorage==null) return false;
if(!worldStorage.exists()){
worldStorage.getParentFile().mkdirs();
}
//"f*ck gson" variable
Map<String,String> tmp = new HashMap<>();

try(FileWriter writer = new FileWriter(worldStorage)){
Gson gson = new Gson();
blockCache.entrySet().forEach(entry->{
tmp.put(gson.toJson(entry.getKey()),gson.toJson(entry.getValue()));
});
String result = gson.toJson(tmp);

writer.write(result);
} catch(Exception e) {
return false;
}
return true;
}

public static boolean load(){
File worldStorage = FileName();

if(worldStorage==null) return false;
if(!worldStorage.exists()) return true;
//"f*ck gson" variable
Map<String,String> tmp = new HashMap<>();

try(JsonReader reader = new JsonReader( new FileReader(worldStorage))){
Gson gson = new Gson();
blockCache.clear();
tmp = gson.fromJson(reader,blockCache.getClass());
tmp.entrySet().forEach(entry ->{
BlockPos pos = gson.fromJson(entry.getKey(),new TypeToken<BlockPos>() {}.getType());
Identifier id = gson.fromJson(entry.getValue(),new TypeToken<Identifier>() {}.getType());
blockCache.put(pos,id);

});
} catch(Exception e) {
return false;
}
return true;
}

private static File FileName(){
Minecraft client = Minecraft.getInstance();
if (client.getCurrentServer()!=null){
return new File(StorageDir,client.getCurrentServer().ip.replace(".","_").replace(":","-").toString()+".json");

}
else if(client.getSingleplayerServer()!=null){
return new File(StorageDir,client.getSingleplayerServer().getWorldPath(LevelResource.ROOT).getParent().getFileName().toString()+".json");
}
else return null;
}
}
52 changes: 52 additions & 0 deletions src/main/java/ru/Jor/BeaconRadius/BeaconRadius.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ru.Jor.BeaconRadius;


import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLevelEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.client.Minecraft;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.level.block.Blocks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



import java.awt.*;

public class BeaconRadius implements ModInitializer {

public static final String MOD_ID = "beaconradius";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

private static BeaconRadius instance;

public static BeaconRadius getInstance() {
return instance;
}

@Override
public void onInitialize() {
instance = this;
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {

if (Minecraft.getInstance().options.keyShift.isDown()) {
if (world.getBlockState(hitResult.getBlockPos()).is(Blocks.BEACON)) {
if(world.isClientSide()) {
BRStorage.Toggle(hitResult.getBlockPos(),world.dimension().identifier());
}
return InteractionResult.SUCCESS;
}
}
return InteractionResult.PASS;
});

ClientLevelEvents.AFTER_CLIENT_LEVEL_CHANGE.register((player, world)->{
if(player.getCurrentServer()!=null||player.getSingleplayerServer()!=null){
BRStorage.onJoin();
}
});
LOGGER.info("Mod {} initialized!", MOD_ID);
}

}
21 changes: 21 additions & 0 deletions src/main/java/ru/Jor/BeaconRadius/client/BeaconRadiusClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.Jor.BeaconRadius.client;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderEvents;

import ru.Jor.BeaconRadius.BeaconRadius;
public class BeaconRadiusClient implements ClientModInitializer {

@Override
public void onInitializeClient(){
BeaconRadius.LOGGER.info("Client side of mod {} initialized!", BeaconRadius.MOD_ID);
LevelRenderEvents.BEFORE_TRANSLUCENT_TERRAIN.register(RenderAndDraw::renderAndDraw);
}







}
Loading